<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: SQL dump to Excel in Python Questions</title>
    <link>https://community.esri.com/t5/python-questions/sql-dump-to-excel/m-p/527892#M41361</link>
    <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Throw in a few print() statements along the way or use a try/accept block and see it tosses something for you.&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
    <pubDate>Tue, 14 Jul 2020 18:58:34 GMT</pubDate>
    <dc:creator>JoeBorgione</dc:creator>
    <dc:date>2020-07-14T18:58:34Z</dc:date>
    <item>
      <title>SQL dump to Excel</title>
      <link>https://community.esri.com/t5/python-questions/sql-dump-to-excel/m-p/527886#M41355</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;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?&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Error line 14&lt;/P&gt;&lt;P&gt;'utf8' codec can't decode byte 0xc3 in position 0: invalid continuation byte&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;&lt;CODE&gt;&lt;SPAN class="keyword token"&gt;import&lt;/SPAN&gt; pyodbc
&lt;SPAN class="keyword token"&gt;import&lt;/SPAN&gt; pandas &lt;SPAN class="keyword token"&gt;as&lt;/SPAN&gt; pd
&lt;SPAN class="keyword token"&gt;import&lt;/SPAN&gt; os
cnxn &lt;SPAN class="operator token"&gt;=&lt;/SPAN&gt; pyodbc&lt;SPAN class="punctuation token"&gt;.&lt;/SPAN&gt;connect&lt;SPAN class="punctuation token"&gt;(&lt;/SPAN&gt;&lt;SPAN class="string token"&gt;"Driver={SQL Server};"&lt;/SPAN&gt;
                      &lt;SPAN class="string token"&gt;"Server=***;"&lt;/SPAN&gt;
                      &lt;SPAN class="string token"&gt;"Database=***;"&lt;/SPAN&gt;
                      &lt;SPAN class="string token"&gt;"uid=***;pwd=***"&lt;/SPAN&gt;&lt;SPAN class="punctuation token"&gt;)&lt;/SPAN&gt;
cursor &lt;SPAN class="operator token"&gt;=&lt;/SPAN&gt; cnxn&lt;SPAN class="punctuation token"&gt;.&lt;/SPAN&gt;cursor&lt;SPAN class="punctuation token"&gt;(&lt;/SPAN&gt;&lt;SPAN class="punctuation token"&gt;)&lt;/SPAN&gt;
script &lt;SPAN class="operator token"&gt;=&lt;/SPAN&gt; &lt;SPAN class="string token"&gt;"""
SELECT * FROM ***.***
"""&lt;/SPAN&gt;
df &lt;SPAN class="operator token"&gt;=&lt;/SPAN&gt; pd&lt;SPAN class="punctuation token"&gt;.&lt;/SPAN&gt;read_sql&lt;SPAN class="punctuation token"&gt;(&lt;/SPAN&gt;script&lt;SPAN class="punctuation token"&gt;,&lt;/SPAN&gt; cnxn&lt;SPAN class="punctuation token"&gt;)&lt;/SPAN&gt;
writer &lt;SPAN class="operator token"&gt;=&lt;/SPAN&gt; pd&lt;SPAN class="punctuation token"&gt;.&lt;/SPAN&gt;ExcelWriter&lt;SPAN class="punctuation token"&gt;(&lt;/SPAN&gt;r&lt;SPAN class="string token"&gt;'C:\Temp\export.xlsx'&lt;/SPAN&gt;&lt;SPAN class="punctuation token"&gt;)&lt;/SPAN&gt;
df&lt;SPAN class="punctuation token"&gt;.&lt;/SPAN&gt;to_excel&lt;SPAN class="punctuation token"&gt;(&lt;/SPAN&gt;writer&lt;SPAN class="punctuation token"&gt;,&lt;/SPAN&gt; sheet_name &lt;SPAN class="operator token"&gt;=&lt;/SPAN&gt;&lt;SPAN class="string token"&gt;'Sheet1'&lt;/SPAN&gt;&lt;SPAN class="punctuation token"&gt;,&lt;/SPAN&gt; encoding&lt;SPAN class="operator token"&gt;=&lt;/SPAN&gt;&lt;SPAN class="string token"&gt;'utf-8'&lt;/SPAN&gt;&lt;SPAN class="punctuation token"&gt;)&lt;/SPAN&gt;
writer&lt;SPAN class="punctuation token"&gt;.&lt;/SPAN&gt;save&lt;SPAN class="punctuation token"&gt;(&lt;/SPAN&gt;&lt;SPAN class="punctuation token"&gt;)&lt;/SPAN&gt;
&lt;SPAN class="line-numbers-rows"&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/CODE&gt;&lt;/PRE&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sat, 11 Dec 2021 22:59:39 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/sql-dump-to-excel/m-p/527886#M41355</guid>
      <dc:creator>CCWeedcontrol</dc:creator>
      <dc:date>2021-12-11T22:59:39Z</dc:date>
    </item>
    <item>
      <title>Re: SQL dump to Excel</title>
      <link>https://community.esri.com/t5/python-questions/sql-dump-to-excel/m-p/527887#M41356</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;I just dealt with that same problem see:&amp;nbsp;&lt;A href="https://community.esri.com/thread/256232" target="_blank"&gt;Unicode Errors&lt;/A&gt;&amp;nbsp; &amp;nbsp;Line14, change&amp;nbsp;&lt;/P&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;&lt;CODE&gt;encoding&lt;SPAN class="" style="color: #a67f59; background: rgba(255, 255, 255, 0.5); border: 0px; font-weight: inherit;"&gt;=&lt;/SPAN&gt;&lt;SPAN class="" style="color: #669900; border: 0px; font-weight: inherit;"&gt;'utf-8'
&lt;/SPAN&gt;&lt;/CODE&gt;#to
encoding='utf-16'&lt;/PRE&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sat, 11 Dec 2021 22:59:41 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/sql-dump-to-excel/m-p/527887#M41356</guid>
      <dc:creator>JoeBorgione</dc:creator>
      <dc:date>2021-12-11T22:59:41Z</dc:date>
    </item>
    <item>
      <title>Re: SQL dump to Excel</title>
      <link>https://community.esri.com/t5/python-questions/sql-dump-to-excel/m-p/527888#M41357</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;utf-16 didn't work, adding reload(sys) , sys.setdefaultencoding('Cp1252') to my code worked.&lt;/P&gt;&lt;P&gt;Thanks tho.&lt;/P&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;&lt;CODE&gt;import pyodbc, sys
import pandas as pd
import os
reload(sys)  
sys.setdefaultencoding('Cp1252')‍‍‍‍‍&lt;SPAN class="line-numbers-rows"&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/CODE&gt;&lt;/PRE&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sat, 11 Dec 2021 22:59:44 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/sql-dump-to-excel/m-p/527888#M41357</guid>
      <dc:creator>CCWeedcontrol</dc:creator>
      <dc:date>2021-12-11T22:59:44Z</dc:date>
    </item>
    <item>
      <title>Re: SQL dump to Excel</title>
      <link>https://community.esri.com/t5/python-questions/sql-dump-to-excel/m-p/527889#M41358</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Ah, yes I've used that too.&amp;nbsp; Glad you got it going none the less; those errors are a pain.&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Tue, 14 Jul 2020 13:16:44 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/sql-dump-to-excel/m-p/527889#M41358</guid>
      <dc:creator>JoeBorgione</dc:creator>
      <dc:date>2020-07-14T13:16:44Z</dc:date>
    </item>
    <item>
      <title>Re: SQL dump to Excel</title>
      <link>https://community.esri.com/t5/python-questions/sql-dump-to-excel/m-p/527890#M41359</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;The SQL Server default collation for many Windows locales is&amp;nbsp;&lt;SPAN style="color: #171717; font-family: 'Segoe UI', SegoeUI, 'Helvetica Neue', Helvetica, Arial, sans-serif; font-size: 14px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: left; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: #ffffff; text-decoration-style: initial; text-decoration-color: initial; display: inline !important; float: none;"&gt;Latin1_General_CI_AS&lt;/SPAN&gt; .&amp;nbsp; For English(United States), the default collation &lt;SPAN style="color: #171717; font-family: 'Segoe UI', SegoeUI, 'Helvetica Neue', Helvetica, Arial, sans-serif; font-size: 14px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: left; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: #ffffff; text-decoration-style: initial; text-decoration-color: initial; display: inline !important; float: none;"&gt;SQL_Latin1_General_CP1_CI_AS&lt;/SPAN&gt;. Both of those collations use code page 1252 (CP-1252).&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;A class="link-titled" href="https://docs.microsoft.com/en-us/sql/relational-databases/collations/collation-and-unicode-support?view=sql-server-2017" title="https://docs.microsoft.com/en-us/sql/relational-databases/collations/collation-and-unicode-support?view=sql-server-2017"&gt;Collation and Unicode support - SQL Server | Microsoft Docs&lt;/A&gt;&amp;nbsp;&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Tue, 14 Jul 2020 13:51:13 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/sql-dump-to-excel/m-p/527890#M41359</guid>
      <dc:creator>JoshuaBixby</dc:creator>
      <dc:date>2020-07-14T13:51:13Z</dc:date>
    </item>
    <item>
      <title>Re: SQL dump to Excel</title>
      <link>https://community.esri.com/t5/python-questions/sql-dump-to-excel/m-p/527891#M41360</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;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&amp;nbsp; 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?&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;&lt;CODE&gt;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()&lt;SPAN class="line-numbers-rows"&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/CODE&gt;&lt;/PRE&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sat, 11 Dec 2021 22:59:46 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/sql-dump-to-excel/m-p/527891#M41360</guid>
      <dc:creator>CCWeedcontrol</dc:creator>
      <dc:date>2021-12-11T22:59:46Z</dc:date>
    </item>
    <item>
      <title>Re: SQL dump to Excel</title>
      <link>https://community.esri.com/t5/python-questions/sql-dump-to-excel/m-p/527892#M41361</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Throw in a few print() statements along the way or use a try/accept block and see it tosses something for you.&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Tue, 14 Jul 2020 18:58:34 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/sql-dump-to-excel/m-p/527892#M41361</guid>
      <dc:creator>JoeBorgione</dc:creator>
      <dc:date>2020-07-14T18:58:34Z</dc:date>
    </item>
  </channel>
</rss>

