简体   繁体   中英

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:

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

Error occurs because you are not specifying the encoding

Try this:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM