Skip to content

Instantly share code, notes, and snippets.

@Thesharing
Created June 22, 2019 07:11
Show Gist options
  • Select an option

  • Save Thesharing/ae099426730a1b1806c62249c0efccee to your computer and use it in GitHub Desktop.

Select an option

Save Thesharing/ae099426730a1b1806c62249c0efccee to your computer and use it in GitHub Desktop.
Pandas Dataframe to_csv() 方法中文乱码解决方案

在用pandas.DataFrame.to_csv()方法时,如果使用默认编码或者指定编码为'utf-8'时中文会乱码。

import pandas as pd

df = pd.DataFrame()
df.to_csv(encoding='utf-8')

问题的根源在于文件开头没有BOM。

将编码改成utf_8_sig就不会乱码了。

df.to_csv(file_path, encoding='utf_8_sig')

Reference: https://blog.csdn.net/weixin_39461443/article/details/75303072

@qqwqqw689
Copy link

有用!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment