sor*_*bet 7 python encoding cx-oracle
我正在使用包含大量中文字符的数据库.我的代码是这样的:
connection = cx_Oracle.connect("%s/%s@%s:%s/%s" % (username, password, host, port, service_name))
cursor = connection.cursor()
cursor.execute('SELECT HOTEL_ID,CREATE_TIME,SOURCE,CONTENT,TITLE,RATE,UPDATE_TIME FROM T_FX_COMMENTS')
for row in cursor:
# Stuff goes here
pass
Run Code Online (Sandbox Code Playgroud)
但我得到这个错误:
Traceback (most recent call last):
File "test.py", line 17, in <module>
for row in cursor:
UnicodeDecodeError: 'gbk' codec can't decode byte 0xaf in position 26: illegal multibyte sequence
Run Code Online (Sandbox Code Playgroud)
这似乎GBK还不够.我想让cx-oracle我给出GB18030编码结果,而不是GBK.我该怎么做呢?
cx_Oracle.Connection.encoding是只读的...我没有在cx-oracle文档中找到任何暗示我可以做到这一点的内容.
我使用的是Python 3.3.2和cx-oracle5.1.2.我必须在这里找到一些东西.感谢帮助!
尝试在程序开头设置 NLS_LANG 环境变量:
import os
os.environ["NLS_LANG"] = ".GB18030"
Run Code Online (Sandbox Code Playgroud)