简体   繁体   English

如何在不获取UnicodeEncodeError的情况下将pandas数据帧转换为csv文件

[英]How can I convert a pandas dataframe to an csv file without getting the UnicodeEncodeError

I am trying to export a pandas dataframe to a csv file using the to_csv() as: 我正在尝试使用to_csv()将pandas数据框导出到csv文件中,如下所示:

export_file_path=fd.asksaveasfile(filetypes=[("CSV File","*.csv"),("All Files","*.*")],defaultextension="*.csv",initialdir=fDir,initialfile="Untitled")
if export_file_path:
    df.to_csv(export_file_path)

Getting this error: 收到此错误:

UnicodeEncodeError: 'charmap' codec can't encode character '\♀' in position 13: character maps to UnicodeEncodeError:'charmap'编解码器无法在位置13编码字符'\\ u2640':字符映射到

Error occurs because you are not specifying the encoding 发生错误,因为您未指定编码

Try this: 尝试这个:

df.to_csv(file_name, sep='\\t', encoding='utf-8')

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何将熊猫数据框保存到压缩的csv文件中? - how can I save a Pandas dataframe into a compressed csv file? When converting a pandas dataframe to csv, how can i seperate the headers of the dataframe into different columns of the csv-file? - When converting a pandas dataframe to csv, how can i seperate the headers of the dataframe into different columns of the csv-file? 如何将 a.mat 文件转换为 pandas dataFrame(通过字典)? - How can I convert a .mat file into a pandas dataFrame (via a dictionary)? 如何将结构文本文件转换为熊猫数据框 - How can I convert a structure text file into a pandas dataframe 如何将大型JSON文件转换为Pandas Dataframe或常规CSV文件? - How do I convert a large JSON file to a Pandas Dataframe or a regular CSV file? 我如何将从制表符分隔文件中获得的 CSV 转换为 pandas dataframe - how would I convert CSV obtained from tab separated file into pandas dataframe 如何使用pandas.read_csv将CSV文件中的数据插入数据框? - How can I insert data from a CSV file into a dataframe using pandas.read_csv? 如何将此字典转换为 Pandas dataframe? - How can I convert this dictionary into a Pandas dataframe? 如何将此 pandas dataframe 转换为列表? - How can I convert this pandas dataframe to a list? 我可以在不使用 ZE1E1D3D40573127D6EE048 的 python 中将 a.rda 文件转换为 pandas dataframe 吗? - Can I convert a .rda file to a pandas dataframe in python without using R?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM