简体   繁体   中英

Python pandas to excel UnicodeDecodeError: 'ascii' codec can't decode byte 0xe2 in position 11

After the web scraping of an e-commerce web site I have saved all the data into a pandas dataframe. Well, when I'm trying to save my pandas dataframe to an excel file but I get the following error:

Traceback (most recent call last):

File "<ipython-input-7-3dafdf6b87bd>", line 2, in <module>
  sheet_name='Dolci', encoding='iso-8859-1')

File "C:\ProgramData\Anaconda2\lib\site-packages\pandas\core\frame.py", line 
  1466, in to_excel
     excel_writer.save()

File "C:\ProgramData\Anaconda2\lib\site-packages\pandas\io\excel.py", line 
  1502, in save
     return self.book.close()

File "C:\ProgramData\Anaconda2\lib\site-packages\xlsxwriter\workbook.py", 
  line 299, in close
     self._store_workbook()

File "C:\ProgramData\Anaconda2\lib\site-packages\xlsxwriter\workbook.py", 
  line 607, in _store_workbook
     xml_files = packager._create_package()

File "C:\ProgramData\Anaconda2\lib\site-packages\xlsxwriter\packager.py", 
  line 139, in _create_package
     self._write_shared_strings_file()

File "C:\ProgramData\Anaconda2\lib\site-packages\xlsxwriter\packager.py", 
  line 286, in _write_shared_strings_file
     sst._assemble_xml_file()

File "C:\ProgramData\Anaconda2\lib\site-
  packages\xlsxwriter\sharedstrings.py", line 53, in _assemble_xml_file
     self._write_sst_strings()

File "C:\ProgramData\Anaconda2\lib\site-
  packages\xlsxwriter\sharedstrings.py", line 83, in _write_sst_strings
     self._write_si(string)

File "C:\ProgramData\Anaconda2\lib\site-
  packages\xlsxwriter\sharedstrings.py", line 110, in _write_si
     self._xml_si_element(string, attributes)

File "C:\ProgramData\Anaconda2\lib\site-packages\xlsxwriter\xmlwriter.py", 
  line 122, in _xml_si_element
     self.fh.write("""<si><t%s>%s</t></si>""" % (attr, string))

File "C:\ProgramData\Anaconda2\lib\codecs.py", line 706, in write
     return self.writer.write(data)

File "C:\ProgramData\Anaconda2\lib\codecs.py", line 369, in write
     data, consumed = self.encode(object, self.errors)


UnicodeDecodeError: 'ascii' codec can't decode byte 0xe2 in position 11: 
ordinal not in range(128)

The code I use is this:

df.to_excel('my_file.xlsx',sheet_name='Dolci', encoding='iso-8859-1')

but it doesn't work, I even have tried:

df.to_excel('my_file.xlsx',sheet_name='Dolci', encoding='utf-8')

but it still give me error. Can somebody help me on this issue?

It seems like you use xlsxwriter engine in ExcelWriter. Try to use openpyxl instead.

writer = pd.ExcelWriter('file_name.xlsx', engine='openpyxl')
df.to_excel(writer)
writer.save()

There is an essential param of to_excel method, try

df.to_excel('filename.xlsx', engine='openpyxl')

and it works for me.

Adding to @Vadym 's response, you may have to close your writer to get the file to be created.

    writer = pd.ExcelWriter(xlPath, engine='openpyxl')
    df.to_excel(writer)
    writer.close()

"depends on the behaviour of the used engine"

See: https://github.com/pandas-dev/pandas/issues/9145 This should be a comment but I don't have the rep...

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