简体   繁体   中英

pyodbc remove unicode strings

I'm using pyodbc to connect sqlserver and below is my connection string..Everything is proper but the results are returned as a unicode string..I have the CHARSET=UTF8 in the connection string but still its returning as unicode string?

Is there any way that I can limit it using the connection paramter itself?

I don't want to call a extra function to convert my unicode to normal strings.

import pyodbc as p

connstr= 'DRIVER={SQL Server};SERVER=USERNAME\SQLEXPRESS;DATABASE=TEST;Trusted_Connection=yes;unicode_results=True;CHARSET=UTF8'
conn = p.connect(connstr)
print conn
cursor = conn.cursor()
result = cursor.execute("select * from employee1")
for each in result:
    print each

You can not handle this issue in the connection string. SQL Server doesn't have a CHARSET property in it's odbc connection settings, so that won't do you any good.

The overall issue you are having is that the data IS unicode in the database. The data type for that column is nvarchar, it is an extended (UTF-16... could be UC-2 in windows, can't remember) data type to include international data characters.

Your options are to convert the data via cast in the select query, eg:

SELECT CAST(fieldname AS VARCHAR) AS fieldname

or convert it in python, eg:

# to utf-8
row.fieldname.encode('utf8')

# to ascii, ignore non-utf-8 characters
row.fieldname.encode('ascii', 'ignore')

# to ascii, replace non-utf-8 characters with ?
row.fieldname.encode('ascii', 'replace')

If you don't need international characters, then you could store the data in varchar instead of nvarchar.

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