pandas 将包含汉字的熊猫数据框保存到文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/43321243/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 03:21:58  来源:igfitidea点击:

Save pandas dataframe containing Chinese character to file

pythonpandasdataframespecial-characters

提问by Yiliang

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

我有一个Pandas数据框,其中一些字段包含汉字。我使用以下代码:

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.

然后我使用excel或文本编辑器打开saved.csv。所有的汉字都变成了垃圾字符。但是,我能够加载保存的文件并正确显示中文,如下所示。

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

Does anyone know how to solve the problem?

有谁知道如何解决问题?

回答by Kenji

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-8failed

utf-8失败时它对我有用

回答by Yiliang

The problem is with the encoding of excel.

问题在于excel的编码。

To resolve the issue, I first open the csv using sublime and then File->Save with encoding->UTF-8 with BOM (Byte Order Mark).

为了解决这个问题,我首先使用 sublime 打开 csv,然后 File->Save with encoding->UTF-8 with BOM (Byte Order Mark)。

Now excel is able to open the csv without any problems!

现在 excel 可以毫无问题地打开 csv 了!