如何使用pyodbc编码Unicode字符串以保存到SAS数据集? [英] How do I encode Unicode strings using pyodbc to save to a SAS dataset?

查看:139
本文介绍了如何使用pyodbc编码Unicode字符串以保存到SAS数据集?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Python使用pyodbc和SAS ODBC驱动程序读取和写入SAS数据集.我可以很好地加载数据,但是当我保存数据时,可以使用类似以下的方法:

I'm using Python to read and write SAS datasets, using pyodbc and the SAS ODBC drivers. I can load the data perfectly well, but when I save the data, using something like:

cursor.execute('insert into dataset.test VALUES (?)', u'testing')

...我收到pyodbc.Error: ('HY004', '[HY004] [Microsoft][ODBC Driver Manager] SQL data type out of range (0) (SQLBindParameter)')错误.

问题似乎是我传递了一个unicode字符串;我需要怎么做才能解决这个问题?

The problem seems to be the fact I'm passing a unicode string; what do I need to do to handle this?

推荐答案

您知道数据库期望使用什么字符编码吗?如果是这样,您可以在执行查询之前尝试对Unicode字符串进行编码.因此,如果您的数据库需要utf-8字符串,则可以尝试执行以下操作:

Do you know what character encoding your database is expecting? If so, you could try encoding your Unicode string before executing the query. So if your database is expecting utf-8 strings, you could try something like:

encoding = 'utf-8' # or latin1 or cp1252 or something
s = u'testing'.encode(encoding)
cursor.execute('insert into dataset.test VALUES (?)', s)

这篇关于如何使用pyodbc编码Unicode字符串以保存到SAS数据集?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆