Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python DataFrame.to_csv causes characters with accents to load incorrectly in excel

I have an issue where character with an accent such as the 'ë' in 'Citroën' are being read as 'Citroën' when I open the files in excel.

enter image description here

like image 471
cwfmoore Avatar asked Oct 18 '25 13:10

cwfmoore


2 Answers

I suspect the issue is to do with the encoding of the output file.

According to the docs the default encoding for the to_csv function is 'utf-8'. Try using 'utf-8-sig' instead, i.e.:

df.to_csv('output.csv', index = False, encoding = 'utf-8-sig')

That often does the trick for me.

like image 151
Fab Dot Avatar answered Oct 21 '25 04:10

Fab Dot


You can also use utf-16, it works for me, but if you have a problem with the separator option, do as it follows:

df.to_csv('output.csv', encoding='utf-16', sep='\t', index=False)
like image 33
chronowix Avatar answered Oct 21 '25 02:10

chronowix