<?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 UnicodeEncodeError in Python Questions</title>
    <link>https://community.esri.com/t5/python-questions/unicodeencodeerror/m-p/1011894#M59375</link>
    <description>&lt;P&gt;I have written a script that that (should) step through a list of all the tables in a mySql database and write the contents of each to a csv table.&amp;nbsp; I've used this basic construct successfully in the past but with specified tables.&amp;nbsp; This time I'm looping through all the tables in the given database and I'm encountering Unicode errors.&amp;nbsp; For example:&lt;/P&gt;&lt;P&gt;UnicodeEncodeError: 'charmap' codec can't encode character '\x9d' in position 646: character maps to &amp;lt;undefined&amp;gt;&lt;/P&gt;&lt;P&gt;The script roles along and creates a couple dozen csv files without a hitch so I looked at what has been created and figured the next table in the list is the culprit.&amp;nbsp; But I get a different unicode error when I run the script on just that table:&lt;/P&gt;&lt;P&gt;UnicodeEncodeError: 'charmap' codec can't encode character '\u0400' in position 1255: character maps to &amp;lt;undefined&amp;gt;&lt;/P&gt;&lt;P&gt;Clearly I stand to see these sorts of errors every time through the loop.&amp;nbsp; Is there way to trap these errors and then exclude the offending character from the output?&lt;/P&gt;&lt;P&gt;Here's the code, head to toe:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="python"&gt;# -*- coding: utf-8 -*-
"""
Created on Tue Dec 22 12:37:49 2020

@author: jborgione
"""

import mysql.connector as mysql
from mysql.connector import FieldType
import csv,os



outFolder = r'N:\GIS\E360'            


def createCSV(table):
    cn = mysql.connect(user = 'users', password = 'password', host = 'host', database = 'master_db')
    cursor = cn.cursor()
    table = 'complaints'
    query = f'SELECT * from {table};'
                          
                                     
    cursor.execute(query)
            
    rows = cursor.fetchall()
    column_names = [i[0] for i in cursor.description]
    dotcsv = f'{table}.csv'
    
    outFile = open(os.path.join(outFolder,dotcsv),'w',newline ='')
    myFile = csv.writer(outFile)
    myFile.writerow(column_names)
    myFile.writerows(rows)
    cursor.close




def main():
    tableList = []
    cn = mysql.connect(user = 'user', password = 'password', host = 'host', database = 'master_db')
    cursor = cn.cursor()
    allTables = "show tables"
    cursor.execute(allTables)
    for (allTables) in cursor:
        tableList.append(allTables[0])
    cursor.close
    
    for table in tableList:
        createCSV(table)
 
if __name__ == '__main__':  #if def main exists, call it
    main() &lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Tue, 22 Dec 2020 21:52:11 GMT</pubDate>
    <dc:creator>JoeBorgione</dc:creator>
    <dc:date>2020-12-22T21:52:11Z</dc:date>
    <item>
      <title>UnicodeEncodeError</title>
      <link>https://community.esri.com/t5/python-questions/unicodeencodeerror/m-p/1011894#M59375</link>
      <description>&lt;P&gt;I have written a script that that (should) step through a list of all the tables in a mySql database and write the contents of each to a csv table.&amp;nbsp; I've used this basic construct successfully in the past but with specified tables.&amp;nbsp; This time I'm looping through all the tables in the given database and I'm encountering Unicode errors.&amp;nbsp; For example:&lt;/P&gt;&lt;P&gt;UnicodeEncodeError: 'charmap' codec can't encode character '\x9d' in position 646: character maps to &amp;lt;undefined&amp;gt;&lt;/P&gt;&lt;P&gt;The script roles along and creates a couple dozen csv files without a hitch so I looked at what has been created and figured the next table in the list is the culprit.&amp;nbsp; But I get a different unicode error when I run the script on just that table:&lt;/P&gt;&lt;P&gt;UnicodeEncodeError: 'charmap' codec can't encode character '\u0400' in position 1255: character maps to &amp;lt;undefined&amp;gt;&lt;/P&gt;&lt;P&gt;Clearly I stand to see these sorts of errors every time through the loop.&amp;nbsp; Is there way to trap these errors and then exclude the offending character from the output?&lt;/P&gt;&lt;P&gt;Here's the code, head to toe:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="python"&gt;# -*- coding: utf-8 -*-
"""
Created on Tue Dec 22 12:37:49 2020

@author: jborgione
"""

import mysql.connector as mysql
from mysql.connector import FieldType
import csv,os



outFolder = r'N:\GIS\E360'            


def createCSV(table):
    cn = mysql.connect(user = 'users', password = 'password', host = 'host', database = 'master_db')
    cursor = cn.cursor()
    table = 'complaints'
    query = f'SELECT * from {table};'
                          
                                     
    cursor.execute(query)
            
    rows = cursor.fetchall()
    column_names = [i[0] for i in cursor.description]
    dotcsv = f'{table}.csv'
    
    outFile = open(os.path.join(outFolder,dotcsv),'w',newline ='')
    myFile = csv.writer(outFile)
    myFile.writerow(column_names)
    myFile.writerows(rows)
    cursor.close




def main():
    tableList = []
    cn = mysql.connect(user = 'user', password = 'password', host = 'host', database = 'master_db')
    cursor = cn.cursor()
    allTables = "show tables"
    cursor.execute(allTables)
    for (allTables) in cursor:
        tableList.append(allTables[0])
    cursor.close
    
    for table in tableList:
        createCSV(table)
 
if __name__ == '__main__':  #if def main exists, call it
    main() &lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 22 Dec 2020 21:52:11 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/unicodeencodeerror/m-p/1011894#M59375</guid>
      <dc:creator>JoeBorgione</dc:creator>
      <dc:date>2020-12-22T21:52:11Z</dc:date>
    </item>
    <item>
      <title>Re: UnicodeEncodeError</title>
      <link>https://community.esri.com/t5/python-questions/unicodeencodeerror/m-p/1011902#M59376</link>
      <description>&lt;P&gt;After some google fishing I added a couple of things that got me past the errors:&lt;/P&gt;&lt;LI-CODE lang="python"&gt;''' not entirely sure what this does but I remember it from an earlier post I made sometime back using python 3.6, the old reload(sys) does not work'''

import importlib
importlib.reload(sys)

''' added encoding = 'utf-8', errors = 'ignore''''
outFile = open(os.path.join(outFolder,dotcsv),'w',encoding = 'utf-8', errors = 'ignore', newline ='')&lt;/LI-CODE&gt;</description>
      <pubDate>Tue, 22 Dec 2020 22:14:53 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/unicodeencodeerror/m-p/1011902#M59376</guid>
      <dc:creator>JoeBorgione</dc:creator>
      <dc:date>2020-12-22T22:14:53Z</dc:date>
    </item>
    <item>
      <title>Re: UnicodeEncodeError</title>
      <link>https://community.esri.com/t5/python-questions/unicodeencodeerror/m-p/1011903#M59377</link>
      <description>&lt;P&gt;csv and other crossover modules aren't unicode compliant by default&lt;/P&gt;&lt;P&gt;&lt;A href="https://stackoverflow.com/questions/18766955/how-to-write-utf-8-in-a-csv-file/31642070" target="_blank"&gt;python - How to write UTF-8 in a CSV file - Stack Overflow&lt;/A&gt;&lt;/P&gt;&lt;P&gt;has this ditty&lt;/P&gt;&lt;LI-CODE lang="python"&gt;import csv

with open('output_file_name', 'w', newline='', encoding='utf-8') as csv_file:
    writer = csv.writer(csv_file, delimiter=';')
    writer.writerow('my_utf8_string')&lt;/LI-CODE&gt;</description>
      <pubDate>Tue, 22 Dec 2020 22:16:07 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/unicodeencodeerror/m-p/1011903#M59377</guid>
      <dc:creator>DanPatterson</dc:creator>
      <dc:date>2020-12-22T22:16:07Z</dc:date>
    </item>
    <item>
      <title>Re: UnicodeEncodeError</title>
      <link>https://community.esri.com/t5/python-questions/unicodeencodeerror/m-p/1011905#M59378</link>
      <description>&lt;P&gt;More details,,,&lt;/P&gt;&lt;P&gt;&lt;A href="https://docs.python.org/3/library/csv.html" target="_blank"&gt;csv — CSV File Reading and Writing — Python 3.9.1 documentation&lt;/A&gt;&lt;/P&gt;&lt;P&gt;just flip back to the python version (3.6 if using Pro 2.5/6 or 3.7 for Pro 2.7)&lt;/P&gt;</description>
      <pubDate>Tue, 22 Dec 2020 22:19:22 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/unicodeencodeerror/m-p/1011905#M59378</guid>
      <dc:creator>DanPatterson</dc:creator>
      <dc:date>2020-12-22T22:19:22Z</dc:date>
    </item>
    <item>
      <title>Re: UnicodeEncodeError</title>
      <link>https://community.esri.com/t5/python-questions/unicodeencodeerror/m-p/1011906#M59379</link>
      <description>&lt;P&gt;This is totally un-ArcGIS Pro for now; just pure python...&lt;/P&gt;</description>
      <pubDate>Tue, 22 Dec 2020 22:21:39 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/unicodeencodeerror/m-p/1011906#M59379</guid>
      <dc:creator>JoeBorgione</dc:creator>
      <dc:date>2020-12-22T22:21:39Z</dc:date>
    </item>
    <item>
      <title>Re: UnicodeEncodeError</title>
      <link>https://community.esri.com/t5/python-questions/unicodeencodeerror/m-p/1011908#M59380</link>
      <description>&lt;P&gt;&lt;EM&gt;&lt;STRONG&gt;csv and other crossover modules aren't unicode compliant by default&lt;/STRONG&gt;&lt;/EM&gt;&lt;/P&gt;&lt;P&gt;Good to know!&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 22 Dec 2020 22:24:00 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/unicodeencodeerror/m-p/1011908#M59380</guid>
      <dc:creator>JoeBorgione</dc:creator>
      <dc:date>2020-12-22T22:24:00Z</dc:date>
    </item>
  </channel>
</rss>

