Skip to content Skip to sidebar Skip to footer

Csv.writer Encoding 'utf-8', But Reading Encoding 'cp1252'

When writing to a file I use the following code. Here it's upper case, but I've also seen the encoding in lower case utf-8. path_to_file = os.path.join(r'C:\Users\jpm\Downloads', '

Solution 1:

The encoding has to be specified on an open as well. The encoding in which a file is opened is platform dependant, it would seem to be cp1252 on windows.

You can check the default platform encoding with this: (on Mac it gives utf-8)

>>>import locale
>>>locale.getpreferredencoding(False)
'UTF-8'
withopen('file', 'r', encoding='utf-8'):
    ...
withopen('file', 'w', encoding='utf-8'):
    ...

Post a Comment for "Csv.writer Encoding 'utf-8', But Reading Encoding 'cp1252'"