简体   繁体   English

python熊猫到excel UnicodeDecodeError:'ascii'编解码器无法解码位置11的字节0xe2

[英]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. 在电子商务网站的网络抓取之后,我将所有数据保存到了pandas数据框中。 Well, when I'm trying to save my pandas dataframe to an excel file but I get the following error: 好吧,当我尝试将我的熊猫数据框保存到一个excel文件中时,出现以下错误:

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. 似乎您在ExcelWriter中使用xlsxwriter引擎。 Try to use openpyxl instead. 尝试改用openpyxl。

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

There is an essential param of to_excel method, try 有一个to_excel方法的基本参数,请尝试

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. 除了@Vadym的响应之外,您可能必须关闭编写器才能获取要创建的文件。

    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... 参见: https : //github.com/pandas-dev/pandas/issues/9145这应该是评论,但我没有代表...

暂无
暂无

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

相关问题 安装熊猫:UnicodeDecodeError:&#39;ascii&#39;编解码器无法解码位置 72 中的字节 0xe2:序号不在范围内(128) - Installing pandas: UnicodeDecodeError: 'ascii' codec can't decode byte 0xe2 in position 72: ordinal not in range(128) 熊猫read_excel奇怪的错误:UnicodeDecodeError:&#39;ascii&#39;编解码器无法解码字节0xe2 - Pandas read_excel strange error: UnicodeDecodeError: 'ascii' codec can't decode byte 0xe2 UnicodeDecodeError:&#39;ascii&#39; 编解码器无法解码位置 14 中的字节 0xe2:在 GAE python 中序号不在范围内(128)? - UnicodeDecodeError: 'ascii' codec can't decode byte 0xe2 in position 14: ordinal not in range(128) in GAE python? Python UnicodeDecodeError:&#39;ascii&#39;编解码器无法解码位置0的字节0xe2:序数不在范围内(128) - Python UnicodeDecodeError: 'ascii' codec can't decode byte 0xe2 in position 0: ordinal not in range(128) Python 3 UnicodeDecodeError:&#39;ascii&#39;编解码器无法解码位置0中的字节0xe2:序数不在范围内(128) - Python 3 UnicodeDecodeError: 'ascii' codec can't decode byte 0xe2 in position 0: ordinal not in range(128) `pip install pandas`给出UnicodeDecodeError:&#39;ascii&#39;编解码器无法解码位置41的字节0xe2:序号不在范围内(128) - `pip install pandas` gives UnicodeDecodeError: 'ascii' codec can't decode byte 0xe2 in position 41: ordinal not in range(128) UnicodeDecodeError:&#39;ascii&#39;编解码器无法解码位置0中的字节0xe2:序号不在范围内(128) - UnicodeDecodeError: 'ascii' codec can't decode byte 0xe2 in position 0: ordinal not in range(128) UnicodeDecodeError:“ascii”编解码器无法解码位置 13 中的字节 0xe2:序号不在范围内(128) - UnicodeDecodeError: 'ascii' codec can't decode byte 0xe2 in position 13: ordinal not in range(128) UnicodeDecodeError:“ ascii”编解码器无法解码位置46的字节0xe2:序数不在范围内 - UnicodeDecodeError: 'ascii' codec can't decode byte 0xe2 in position 46: ordinal not in range UnicodeDecodeError:&#39;ascii&#39;编解码器无法解码位置139中的字节0xe2:序数不在范围内(128) - UnicodeDecodeError: 'ascii' codec can't decode byte 0xe2 in position 139: ordinal not in range(128)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM