3

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?

4
  • Can you give us a MCVE? Commented Dec 7, 2017 at 16:06
  • Show the full trace of the error, we need to see what line it originated on and everything that called it. Commented Dec 7, 2017 at 16:28
  • I'm sorry I did add before the full trace error. Commented Dec 11, 2017 at 9:29
  • Hello guys! Is there anybody who can help me? Commented Dec 12, 2017 at 9:29

3 Answers 3

11

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()
Sign up to request clarification or add additional context in comments.

Comments

4

There is an essential param of to_excel method, try

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

and it works for me.

Comments

1

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...

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.