Print Unicode characters from SQL to Excel using xlwt

I use Python to retrieve data from an MSSQL database using an ODBC connection. Then I try to put the extracted data into an Excel file using xlwt.

However, this generates the following error:

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

I ran a script to simply print the data and found that the abusive character in the database is O with a slash through it. On a python printout, this displays as "\ xd8".

The worksheet encoding for xlwt is set to UTF-8.

Is there any way to get this directly in Excel?

Edit

Full error message below:

 C:\>python dbtest1.py Traceback (most recent call last): File "dbtest1.py", line 24, in <module> ws.write(i,j,item) File "build\bdist.win32\egg\xlwt\Worksheet.py", line 1032, in write File "build\bdist.win32\egg\xlwt\Row.py", line 240, in write File "build\bdist.win32\egg\xlwt\Workbook.py", line 309, in add_str File "build\bdist.win32\egg\xlwt\BIFFRecords.py", line 25, in add_str File "C:\Python27\lib\encodings\utf_8.py", line 16, in decode return codecs.utf_8_decode(input, errors, True) UnicodeDecodeError: 'utf8' codec can't decode byte 0xd8 in position 20: invalid continuation byte 
+1
source share
2 answers

Setting the workbook encoding to "latin-1" seems to have achieved the same:

 wb = xlwt.Workbook(encoding='latin-1') 

(it was previously set to 'UTF-8')

Another answer did not work in my case, as there were other fields that were not strings.

+4
source

The SQL output seems to return strings encoded using ascii. You can convert them to unicode with:

 data = unicode(input_string, 'latin-1') 

Then you can put them in a table using xlwt.

-1
source

Source: https://habr.com/ru/post/895826/


All Articles