Select to view content in your preferred language

SQL dump to Excel

1453
6
07-13-2020 03:33 PM
CCWeedcontrol
Frequent Contributor

I am trying to do a SQL dump of a table to excel but I can't get past the error, what am I doing wrong?

Error line 14

'utf8' codec can't decode byte 0xc3 in position 0: invalid continuation byte

import pyodbc
import pandas as pd
import os
cnxn = pyodbc.connect("Driver={SQL Server};"
                      "Server=***;"
                      "Database=***;"
                      "uid=***;pwd=***")
cursor = cnxn.cursor()
script = """
SELECT * FROM ***.***
"""
df = pd.read_sql(script, cnxn)
writer = pd.ExcelWriter(r'C:\Temp\export.xlsx')
df.to_excel(writer, sheet_name ='Sheet1', encoding='utf-8')
writer.save()
0 Kudos
6 Replies
JoeBorgione
MVP Emeritus

I just dealt with that same problem see: Unicode Errors   Line14, change 

encoding='utf-8'
#to
encoding='utf-16'
That should just about do it....
0 Kudos
CCWeedcontrol
Frequent Contributor

utf-16 didn't work, adding reload(sys) , sys.setdefaultencoding('Cp1252') to my code worked.

Thanks tho.

import pyodbc, sys
import pandas as pd
import os
reload(sys)  
sys.setdefaultencoding('Cp1252')‍‍‍‍‍
0 Kudos
JoeBorgione
MVP Emeritus

Ah, yes I've used that too.  Glad you got it going none the less; those errors are a pain.

That should just about do it....
0 Kudos
JoshuaBixby
MVP Esteemed Contributor

The SQL Server default collation for many Windows locales is Latin1_General_CI_AS .  For English(United States), the default collation SQL_Latin1_General_CP1_CI_AS. Both of those collations use code page 1252 (CP-1252).

Collation and Unicode support - SQL Server | Microsoft Docs 

0 Kudos
CCWeedcontrol
Frequent Contributor

The first time I was using 2.y python, which worked yesterday but today nothing happens when I run the script, no error no output .xlsx.and I didn't change anything, I am lost on this one. So i decided to try it on 3.6 so in 3.6 I have to add from importlib import reload  and comment out sys.setdefaultencoding('Cp1252') but in doing so I get the same error, on line 17 'utf8' codec can't decode byte 0xc3 in position 0: invalid continuation byte. Thoughts?

mport pyodbc
import pandas as pd
import os
from importlib import reload
reload(sys)
#sys.setdefaultencoding('Cp1252')

cnxn = pyodbc.connect("Driver={SQL Server};"
                      "Server=***;"
                      "Database=***;"
                      "uid=***;pwd=***")
cursor = cnxn.cursor()
script = """
SELECT * FROM ***.***
"""
df = pd.read_sql(script, cnxn)
writer = pd.ExcelWriter(r'C:\Temp\export.xlsx')
df.to_excel(writer, sheet_name ='Sheet1', encoding='utf-8')
writer.save()
0 Kudos
JoeBorgione
MVP Emeritus

Throw in a few print() statements along the way or use a try/accept block and see it tosses something for you.

That should just about do it....
0 Kudos