Python pandas to_excel 'utf8' codec can't decode byte
Managed to solve this.
I made a function that goes through my columns that have strings and managed to decode/encode them into utf8 and it now works.
def changeencode(data, cols):
for col in cols:
data[col] = data[col].str.decode('iso-8859-1').str.encode('utf-8')
return data
In my case, the problem was that I was initially reading the CSV file with the wrong encoding (ASCII
instead of cp1252
). Therefore, when pandas tried to write it to an Excel file, it found some characters it couldn't decode.
I solved it by specifying the correct encoding when reading the CSV file.
data = pd.read_csv(fname, encoding='cp1252')
Actually, there is a way to force utf8 encoding by passing a parameter to ExcelWriter:
ew = pandas.ExcelWriter('test.xlsx',options={'encoding':'utf-8'})
sampleList = ['Miño', '1', '2', 'señora']
dataframe = pandas.DataFrame(sampleList)
dataframe.to_excel(ew)
ew.save()