简体   繁体   English

UnicodeEncodeError: 'ascii' codec can't encode character u'\\xe9' in position 54: ordinal not in range(128)

[英]UnicodeEncodeError: 'ascii' codec can't encode character u'\xe9' in position 54: ordinal not in range(128)

I know, it has been asked several times, but none of the answer gave me a solution我知道,已经问过几次了,但没有一个答案给了我解决方案

Here is the code (Python 2.7):这是代码(Python 2.7):

import cx_Oracle
import pandas as pd

connstr = 'MyConstr'
conn = cx_Oracle.connect(connstr)
cur = conn.cursor()

xl = pd.ExcelFile("C:\\TEMP\\for_kkod.xlsx")
df = xl.parse(0)

for i in df.index:
    s = u"insert into MY_TABLE values({0}, '{1}')".format(int(df.iloc[i]['kkod']), df.iloc[i]['kkodnev'])
    print s
    print type(s)
    cur.execute(s)

The result of the 2 prints are this: 2次打印的结果是这样的:

insert into MY_TABLE values(10, 'Készítés')
<type 'unicode'>

As you can see the type of s is unicode but nevertheless I have this error message:如您所见, s 的类型是 unicode 但我仍然收到此错误消息:

UnicodeEncodeError: 'ascii' codec can't encode character u'\xe9' in position 54: ordinal not in range(128)

I have tried with and without u"", with and without encode and decode in all possible ways but still the same error message我已经尝试过使用和不使用 u"",使用和不使用所有可能的方式进行编码和解码,但仍然是相同的错误消息

Any idea?有什么想法吗?

You are feeding a Unicode SQL statement to cursor.execute() .您正在向cursor.execute()提供 Unicode SQL 语句。 That method can only take a bytestring SQL statement .该方法只能采用字节串 SQL 语句

You should not be using string interpolation to insert your Unicode values into the SQL query (which itself is just ASCII).您不应该使用字符串插值将 Unicode 值插入 SQL 查询(它本身只是 ASCII)。 Use query parameters, always!始终使用查询参数!

 s = "insert into MY_TABLE values(:0, :1)"
 cur.execute(s, (int(df.iloc[i]['kkod']), df.iloc[i]['kkodnev']))

Now the values to be inserted are passed in as parameters and it is up to the database adapter to worry about encoding these correcty (as well as properly escaping the values to evade SQL injection issues).现在要插入的值作为参数传入,由数据库适配器负责对这些正确性进行编码(以及正确转义这些值以逃避 SQL 注入问题)。

The above uses numbered (positional) arguments, you can also use named parameters, pass in the values in a dictionary with matching keys:上面使用编号(位置)参数,您也可以使用命名参数,使用匹配键传入字典中的值:

 s = "insert into MY_TABLE values(:kkod, :kkodnev)"
 cur.execute(s, {'kkod': int(df.iloc[i]['kkod']), 'kkodnev': df.iloc[i]['kkodnev']})

You do have to make sure both your connection and your table column is correctly configured to handle Unicode.您必须确保您的连接和表列都正确配置为处理 Unicode。 For example, you'll have to set the NLS_LANG option:例如,您必须设置NLS_LANG选项:

import os
os.environ['NLS_LANG'] = '.AL32UTF8'

Simply use the following parameters for connection:只需使用以下参数进行连接:

connection = cx_Oracle.connect(connectString, encoding="UTF-8",nencoding="UTF-8") connection = cx_Oracle.connect(connectString, encoding="UTF-8",nencoding="UTF-8")

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Python eyed3 UnicodeEncodeError:&#39;ascii&#39;编解码器无法在位置17编码字符u&#39;\\ xe9&#39;:序数不在范围内(128) - Python eyed3 UnicodeEncodeError: 'ascii' codec can't encode character u'\xe9' in position 17: ordinal not in range(128) Python - &#39;ascii&#39; 编解码器无法对位置 5 中的字符 u&#39;\\xe9&#39; 进行编码:序号不在范围内(128) - Python - 'ascii' codec can't encode character u'\xe9' in position 5: ordinal not in range(128) Cassandra:&#39;ascii&#39;编解码器无法在位置218处编码字符u&#39;\\ xe9&#39;:序数不在范围内(128) - Cassandra : 'ascii' codec can't encode character u'\xe9' in position 218: ordinal not in range(128) UnicodeEncodeError:'ascii'编解码器无法编码位置17710中的字符u'\ xe7':序数不在范围内(128) - UnicodeEncodeError: 'ascii' codec can't encode character u'\xe7' in position 17710: ordinal not in range(128) UnicodeEncodeError:&#39;ascii&#39;编解码器无法在位置31编码字符&#39;\\ xe9&#39;:安装金字塔期间序数不在range(128)中 - UnicodeEncodeError: 'ascii' codec can't encode character '\xe9' in position 31: ordinal not in range(128) during installing pyramid UnicodeEncodeError:&#39;ascii&#39;编解码器无法编码字符u&#39;\\ xe9&#39; - UnicodeEncodeError: 'ascii' codec can't encode character u'\xe9' UnicodeEncodeError:&#39;ascii&#39;编解码器不能编码字符u&#39;\\ xe9&#39; - UnicodeEncodeError: 'ascii' codec can't encode character u'\xe9' UnicodeEncodeError:&#39;ascii&#39;编解码器无法对位置34中的字符u&#39;\\ u05a0&#39;进行编码:序数不在范围内(128) - UnicodeEncodeError: 'ascii' codec can't encode character u'\u05a0' in position 34: ordinal not in range(128) UnicodeEncodeError:&#39;ascii&#39;编解码器无法对位置47中的字符u&#39;\\ u2019&#39;进行编码:序数不在范围内(128) - UnicodeEncodeError: 'ascii' codec can't encode character u'\u2019' in position 47: ordinal not in range(128) UnicodeEncodeError: &#39;ascii&#39; codec can&#39;t encode character u&#39;\ц&#39; in position 32: ordinal not in range(128) - UnicodeEncodeError: 'ascii' codec can't encode character u'\u0446' in position 32: ordinal not in range(128)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM