0

I'm trying to get some data from one web page and write these data into xlsx file. Everything seems good but the Encoding error probably raises if it tries to write it into xlsx file during CLOSING the file.

ERROR:

Traceback (most recent call last):
  File "C:/Users/Milano/PycharmProjects/distrelec/crawler.py", line 429, in <module>
    temp_file_to_xlsx()
  File "C:/Users/Milano/PycharmProjects/distrelec/crawler.py", line 119, in temp_file_to_xlsx
    wb.close()    
  File "C:\Python27\lib\site-packages\xlsxwriter\workbook.py", line 295, in close
    self._store_workbook()
  File "C:\Python27\lib\site-packages\xlsxwriter\workbook.py", line 518, in _store_workbook
    xml_files = packager._create_package()
  File "C:\Python27\lib\site-packages\xlsxwriter\packager.py", line 134, in _create_package
    self._write_workbook_file()
  File "C:\Python27\lib\site-packages\xlsxwriter\packager.py", line 174, in _write_workbook_file
    workbook._assemble_xml_file()
  File "C:\Python27\lib\site-packages\xlsxwriter\workbook.py", line 464, in _assemble_xml_file
    self._write_sheets()
  File "C:\Python27\lib\site-packages\xlsxwriter\workbook.py", line 1455, in _write_sheets
    self._write_sheet(worksheet.name, id_num, worksheet.hidden)
  File "C:\Python27\lib\site-packages\xlsxwriter\workbook.py", line 1472, in _write_sheet
    self._xml_empty_tag('sheet', attributes)
  File "C:\Python27\lib\site-packages\xlsxwriter\xmlwriter.py", line 80, in _xml_empty_tag
    self.fh.write("<%s/>" % tag)
  File "C:\Python27\lib\codecs.py", line 694, in write
    return self.writer.write(data)
  File "C:\Python27\lib\codecs.py", line 357, in write
    data, consumed = self.encode(object, self.errors)
UnicodeDecodeError: 'ascii' codec can't decode byte 0xe2 in position 23: ordinal not in range(128)

To find out where is the problem I've edited codecs module:

def write(self, object):

        """ Writes the object's contents encoded to self.stream.
        """
        try:
            data, consumed = self.encode(object, self.errors)
            self.stream.write(data)
        except:
            print object
            print repr(object)
            raise Exception

The output is:

<sheet name="Android PC–APC" sheetId="42" r:id="rId42"/>
'<sheet name="Android PC\xe2\x80\x93APC" sheetId="42" r:id="rId42"/>'
    temp_file_to_xlsx()
  File "C:/Users/Milano/PycharmProjects/distrelec/crawler.py", line 119, in temp_file_to_xlsx
    wb.close()    
  File "C:\Python27\lib\site-packages\xlsxwriter\workbook.py", line 295, in close
    self._store_workbook()
  File "C:\Python27\lib\site-packages\xlsxwriter\workbook.py", line 518, in _store_workbook
    xml_files = packager._create_package()
  File "C:\Python27\lib\site-packages\xlsxwriter\packager.py", line 134, in _create_package
    self._write_workbook_file()
  File "C:\Python27\lib\site-packages\xlsxwriter\packager.py", line 174, in _write_workbook_file
    workbook._assemble_xml_file()
  File "C:\Python27\lib\site-packages\xlsxwriter\workbook.py", line 464, in _assemble_xml_file
    self._write_sheets()
  File "C:\Python27\lib\site-packages\xlsxwriter\workbook.py", line 1455, in _write_sheets
    self._write_sheet(worksheet.name, id_num, worksheet.hidden)
  File "C:\Python27\lib\site-packages\xlsxwriter\workbook.py", line 1472, in _write_sheet
    self._xml_empty_tag('sheet', attributes)
  File "C:\Python27\lib\site-packages\xlsxwriter\xmlwriter.py", line 80, in _xml_empty_tag
    self.fh.write("<%s/>" % tag)
  File "C:\Python27\lib\codecs.py", line 699, in write
    return self.writer.write(data)
  File "C:\Python27\lib\codecs.py", line 363, in write
    raise Exception
Exception

What should I do with that please?

Milano
  • 18,048
  • 37
  • 153
  • 353
  • possible duplicate of [Python - 'ascii' codec can't decode byte](http://stackoverflow.com/questions/9644099/python-ascii-codec-cant-decode-byte) – kenorb Aug 25 '15 at 09:57

2 Answers2

1

You have to decode your input data with the correct encoding, which seems to be 'utf-8'.

Daniel
  • 42,087
  • 4
  • 55
  • 81
1

You may want to look at this:

Example: Simple Unicode with Python 2

user2358582
  • 372
  • 2
  • 10