pyodbc remove unicode strings

百般思念 提交于 2019-11-30 23:55:57

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, e.g.:

SELECT CAST(fieldname AS VARCHAR) AS fieldname

or convert it in python, e.g.:

# 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.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!