The csv module has issues with encoding other than ASCII that are well documented:
This version of the csv module doesn’t support Unicode input. Also, there are currently some issues regarding ASCII NUL characters. Accordingly, all input should be UTF-8 or printable ASCII to be safe;
As you are doing simple reads and writes, you can use the example UnicodeWriter
class from the documentation.
Or, you can simplify your code thus:
import codecs
# ...
with codecs.open(r'C:\Users\L\Desktop\myfile.csv',
mode='w', encoding='utf-8') as outfile:
outfile.write('{}\n'.format(','.join(fields)))
for i in rows:
outfile.write('{}\n'.format(','.join(i)))
Please use raw strings r'C:\Users\L\Desktop\myfile.csv'
when you are using \
as the path separator. This is to prevent things like 'C:\newfile
from being interpreted incorrectly.
You can also use forward slashes (even in Windows), which will eliminate the need to use raw strings.
Alternately, you can use os.path.join
to build your file paths.
The point being, avoid using \
.