0

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', 'c19_Vaccine_Current.csv')

#write to file
with open(path_to_file, 'w', newline='', encoding='UTF-8') as csvfile:
    f = csv.writer(csvfile) 
    #write the headers of the csv file
    f.writerow(['County','AdminCount','AdminCountChange', 'RollAvg', 'AllocDoses', 'FullyVaccinated',                    'FullyVaccinatedChange', 'ReportDate', 'Pop', 'PctVaccinated', 'LHDInventory', 'CommInventory',
                'TotalInventory', 'InventoryDate'])

And to check if the *.csv is in fact utf-8 I open it and read it:

with open(path_to_file, 'r') as r:
    print(r)

I'm expecting the encoding to be utf-8, but I get:

<_io.TextIOWrapper name='C:\\Users\\jpm\\Downloads\\c19_Vaccine_Current.csv' mode='r' encoding='cp1252'>

I pretty much borrowed the code from this answer. And I've also read the doc. It's crucial that I have the *.csv file as utf-8, but that doesn't appear to be the case.

Pfalbaum
  • 586
  • 3
  • 10
  • 26

1 Answers1

2

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'
with open('file', 'r', encoding='utf-8'):
    ...
with open('file', 'w', encoding='utf-8'):
    ...
Robert Kearns
  • 1,631
  • 1
  • 8
  • 15
  • Thanks, I'll give you credit for answering that. But I am still not sure how to save the *.csv with ```utf-8``` using ```csv.writer```. I started another post: https://stackoverflow.com/questions/66676553/unsure-if-csv-file-is-encoded-as-utf-8-using-csv-writer – Pfalbaum Mar 17 '21 at 15:49