Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Save pandas dataframe containing Chinese character to file

I have a pandas dataframe, where some fields contain Chinese character. I use the below code:

df = pd.read_csv('original.csv', encoding='utf-8')
df.to_csv('saved.csv')

Then I use excel or text editor to open saved.csv. All Chinese characters become junk characters. However, I am able to load the saved file and show the Chinese properly as follows.

df = pd.read_csv('saved.csv')
df.head() # Chinese characters are properly displayed.

Does anyone know how to solve the problem?

like image 250
Yiliang Avatar asked Dec 01 '22 15:12

Yiliang


1 Answers

Try the following:

df = pd.read_csv('original.csv', encoding='utf-8')   
df.to_csv('saved.csv', encoding='utf_8_sig')

it works for me when utf-8 failed

like image 82
Kenji Avatar answered Dec 04 '22 01:12

Kenji