thanks in advance. I am still very new to python.
I am attempting to write the results of a pyodbc query using FetchAll to a CSV file. When I ran the code on our stage server, everything works great. On our Live server, the data has a encoding issue. I have been attemtping to encode the results of FetchAll() a few different ways but nothing seems to work. You can;t call it on a list element, or in a for loop, it gets caught on a integer and fails.
Any ideas on a easy way to acheive this? I have looked at some other related tickets and thought I found the answer, but as I mentioned above the encoding is happening on a integer. So I admit part of this is inexperience.
Any help would be appreciated. Here is the relevant code.
f = csv.writer(file(filename, 'wb'))
cnxn = pyodbc.connect(self.connect_string)
c = cnxn.cursor()
c.execute(sql)
rows = c.fetchall()
# rows = [[x.encode('utf-8') for x in row] for row in rows] <---- doesnt work, x.encode is considered an int
if include_headers:
f.writerow([d[0] for d in c.description])
for row in rows:
f.writerows(row)
I ended up using the unicodecsv library and it easily solved the problem.