0

I open a .csv file and I write another .csv file in output. I specified encoding='utf-8' for both files. When I read the input file, in a dictionary, I have an accented character (ì) which I can see in the variables I use, but "ì" becomes "ì" when I write it in the output file.

I create the output line by concatenating some variables, like this: output_line = [name, address, citizenship_flag]

citizenship_flag may be "sì" or "no". In the output file it becomes "ì".

Where am I wrong? Thanks.

Stef
  • 11
  • 1
  • Maybe [this](https://stackoverflow.com/questions/904041/reading-a-utf8-csv-file-with-python) discussion can help. – ad007 Oct 13 '21 at 16:56
  • 1
    The file is correctly written as UTF-8. `'ì'` is `'ì'` written as UTF-8 but read as (probably) Windows-1252 encoding instead. It is the program reading your output file that is the problem (Excel?). Many Windows programs require UTF-8 w/ BOM to read UTF-8 correctly or assume the ANSI encoding, which varies by localized version of Windows. US Windows uses Windows-1252. Use `encoding='utf-8-sig'` to generate the output with a BOM marker that apps like Excel use to recognize and decode UTF-8. – Mark Tolonen Oct 13 '21 at 17:58
  • Thanks! I didn't realize that it was Excel's fault when opening the file. – Stef Oct 14 '21 at 07:05
  • Try opening a blank Excel worksheet, click Data > From Text/CSV... follow the wizard and select the correct encoding – Martin Evans Oct 17 '21 at 16:25

0 Answers0