soham_dhole
soham_dhole

Reputation: 69

How to export pandas dataframe containing strings with accented characters to a CSV file without getting any additional characters in the output file?

I have been trying to export a pandas dataframe which has accented characters in few columns to csv. When printed in the iPython notebook the string looks just fine.
for example, the dataframe is as follows:

  col1 col2
1 001  Éxamplé

but when i'm exporting the dataframe to CSV using df.to_csv function using 'utf-8' encoding the output in CSV file looks as follows:

  col1 col2
1 001  Éxamplé

whereas the desired output is:

  col1 col2
1 001  Éxamplé

How can we do this without getting characters changed?

Upvotes: 2

Views: 2280

Answers (2)

Ahsan Munir
Ahsan Munir

Reputation: 11

df.to_csv(filename,encoding='utf-8-sig')

Try this mode it works for me.

Upvotes: 1

Geom
Geom

Reputation: 1546

This should work in most cases.

data.to_csv("Data.csv", encoding="utf-8")

But its really hard to say what you need without knowing specifics. Is the text in foreign language? What software you're using to view the file after saving it? It might be because of that software and not the file itself. Make sure to check out pythons standard encoding here.

Upvotes: 1

Related Questions