[英]Python: UnicodeDecodeError: 'ascii' codec can't decode byte 0xd0 in position 0: ordinal not in range(128)
Scenario: I have a list of server names in a JSON file that is getting read by the script and put into a dictionary. 场景:我有一个JSON文件中的服务器名称列表,该文件正被脚本读取并放入字典中。 I'm then trying to use those server names in what will become a SQL query.
然后,我试图在将成为SQL查询的服务器中使用这些服务器名称。 However, I'm having a hell of a time with the UTF-8 encoded strings.
但是,我在使用UTF-8编码的字符串时遇到了麻烦。
Error Traceback: 错误回溯:
Traceback (most recent call last):
File "run.py", line 18, in <module>
print(str(len(download.downloadRealmFiles('eu'))) + " EU files downloaded.")
File "/var/www/etherealpost.com/scripts/ahdata/download.py", line 73, in downloadRealmFiles
sql = u"UPDATE realms_lastmodified SET last_modified = '%d', latest_hash = '%s' WHERE region = '%s' AND realm = '%s'" % (lastModified, lastHash.encode('utf-8'), region.encode('utf-8'), realm)
UnicodeDecodeError: 'ascii' codec can't decode byte 0xd0 in position 0: ordinal not in range(128)
The code: 编码:
realm = data['files'][0]['realm']
lastHash = realmFile.split('/')[-2]
lastModified = data['files'][0]['lastModified']
dataURLs.append(realmFile)
sql = u"UPDATE realms_lastmodified SET last_modified = '%d', latest_hash = '%s' WHERE region = '%s' AND realm = '%s'" % (lastModified, lastHash.encode('utf-8'), region.encode('utf-8'), realm.encode('utf-8'))
lastModified is of type long The variable realm
is the one that contains the Unicode characters. lastModified的类型为long变量
realm
是包含Unicode字符的领域。
I'm out of ideas why this isn't working. 我不知道为什么这不起作用。
Don't interpolate strings into a SQL query! 不要将字符串插入SQL查询中! Use SQL parameters instead and leave it up to your database to handle quoting and Unicode values:
改用SQL参数,并将其留给数据库处理引号和Unicode值:
sql = """\
UPDATE realms_lastmodified
SET last_modified=?, latest_hash=?
WHERE region=? AND realm=?
"""
cursor.execute(sql, (lastModified, lastHash, region, realm))
I used ?
我用过
?
as the parameter placeholders here, but it depends on the exact database library used; 作为此处的参数占位符,但这取决于所使用的确切数据库库; you may need to use
%s
as the placeholder instead ( regardless of the type of the column!). 您可能需要改用
%s
作为占位符( 无论列的类型是什么 !)。
Your error specifically is caused by you interpolating encoded bytestrings into a Unicode value. 具体来说,您的错误是由您将编码后的字节串插入Unicode值引起的。 Don't do that either;
也不要那样做。 interpolate, then encode.
插值, 然后编码。 Otherwise, Python attempts to decode the UTF8 bytes using the default codec to get Unicode again, and that fails here.
否则,Python会尝试使用默认编解码器解码UTF8字节以再次获取Unicode,这在这里失败。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.