简体   繁体   中英

Python | Pyodbc | Encoding FetchAll() to Utf-8 with .Encode('utf-8')

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)

我最终使用了unicodecsv库,它很容易解决了问题。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM