I am trying to write a data stored in a dataframe to a csv and am running into the following error:
UnicodeEncodeError: 'charmap' codec can't encode characters in position 202-203: character maps to <undefined>
I looked at this thread and this one and tried their solutions of adding encoding='utf-8'
however this did not seem to help. I was wondering if there is another encoding recommendation/ alternative solution to this problem. The code for my function that writes to the csv is as follows:
def data_to_csv(data, data2):
encode = 'utf-8'
with open('data.csv', 'w') as data_csv:
data.to_csv(path_or_buf=data_csv, encoding=encode)
with open('data2_data.csv', 'w') as data2_csv:
data2.to_csv(path_or_buf=data2_csv, encoding=encode)
Any help would be greatly appreciated!
CodePudding user response:
According to the error message, the content of data
does contain some characters that cannot be encoded as charmap
.
You try to write the data encoded as UTF-8, but in order to do so, you should open/create your .csv files with UTF-8 encoding:
with open('data.csv', 'w', encoding='UTF-8') as data_csv:
should work.