tl;dr I have a complicated SQL query that returns results in .csv format. Unfortunately, the downside to being Canadian, is that a bunch of people just love to add accents to their names.
At the moment, this is what I have. I didn't write the script initially, just trying to make it work to grab the data. The SQL is correct, just truncated by nano.
def getCommentsByGUID(reportDay):
conn = mysql.connector.connect(host = dbServer, user = dbUser, passwd = dbPass, db = tscDBName)
sqlresult = ''
sql = 'select d.documentId, dn.created, dn.notes as Comment, u.login, d.fileName from DocumentInfo d \
inner join documentnotes dn on d.documentId=dn.documentId \
inner join User u on dn.userId=u.userID \
where d.companyId=%d and dn.created>\'%s 00:00:00\' and dn.created <\'%s 23:59:59\';' % (companyID, reportDay, reportDa$
cursor = conn.cursor()
cursor.execute (sql)
sqlresult = cursor.fetchall()
cursor.close ()
reportWriter = csv.writer(open('%sFilename_comments_%s.csv' % (outputDir, reportDay), 'w'), delimiter=',', quotec$
for results in sqlresult:
reportWriter.writerow(results)
Running as is creates a unicode error, such as:
UnicodeEncodeError: 'ascii' codec can't encode character u'\u2019' in position 366: ordinal not in range(128)
Now, after a lot of research, I've come across something like this:
for results in sqlresult:
try:
reportWriter.writerow(results)
except UnicodeEncodeError:
s = list(results)
for item in s:
if isinstance(item, basestring) == True:
a = s.index(item)
unicodedata.normalize('NFKD', item).encode('ascii', 'ignore')
s[a] = item
print item
s = tuple(s)
print s
reportWriter.writerow(s)
Yet still get the same error:
UnicodeEncodeError: 'ascii' codec can't encode character u'\u2019' in position 366: ordinal not in range(128)
Any ideas on what I'm either doing wrong, or what else I can try? Thanks!