Search code examples
encodingutf-8python-3.6export-to-csv

Python Encoding String Issue: String "Île-de-France" is being written to CSV file as "√éle-de-France"


I'm trying to write a string "Île-de-France" to a CSV file, however, what is being written to file is "√éle-de-France". I tried encoding="utf-8" in the open and it didn't work. What am I missing? Thanks!

with open('/tmp/directwrite.csv', 'w', newline='') as csvfile:
    fieldnames = ['location']
    writer = csv.DictWriter(csvfile, fieldnames=fieldnames)

    writer.writeheader()
    writer.writerow({'location': 'Île-de-France'})

Solution

  • It’s an issue with your viewer. If using Excel or some other windows program, useencoding='utf-8-sig'. That writes a signature at the beginning of the file that Windows programs recognize as a UTF-8 file; otherwise, it assumes an ANSI encoding for the file.