简体   繁体   English

Python:UnicodeDecodeError:'ascii'编解码器无法解码位置0的字节0xd0:序数不在范围内(128)

[英]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.

相关问题 Python UnicodeDecodeError:&#39;ascii&#39;编解码器无法解码位置12的字节0xd0:序数不在范围内(128) - Python UnicodeDecodeError: 'ascii' codec can't decode byte 0xd0 in position 12: ordinal not in range(128) snapcraft 给出“ascii”编解码器无法解码位置 0 中的字节 0xd0:序号不在范围内(128) - snapcraft gives 'ascii' codec can't decode byte 0xd0 in position 0: ordinal not in range(128) UnicodeDecodeError:“ascii”编解码器无法解码位置 2 中的字节 0xd1:序数不在范围内(128) - UnicodeDecodeError: 'ascii' codec can't decode byte 0xd1 in position 2: ordinal not in range(128) Python,UnicodeDecodeError:&#39;ascii&#39;编解码器无法解码位置1718的字节0xc2:序数不在范围内(128) - Python, UnicodeDecodeError: 'ascii' codec can't decode byte 0xc2 in position 1718: ordinal not in range(128) UnicodeDecodeError:&#39;ascii&#39; 编解码器无法解码位置 14 中的字节 0xe2:在 GAE python 中序号不在范围内(128)? - UnicodeDecodeError: 'ascii' codec can't decode byte 0xe2 in position 14: ordinal not in range(128) in GAE python? Python UnicodeDecodeError:&#39;ascii&#39;编解码器无法解码位置0的字节0xe2:序数不在范围内(128) - Python UnicodeDecodeError: 'ascii' codec can't decode byte 0xe2 in position 0: ordinal not in range(128) (python/boto sqs)UnicodeDecodeError:“ascii”编解码器无法解码位置 5 中的字节 0xc3:序号不在范围内(128) - (python/boto sqs) UnicodeDecodeError: 'ascii' codec can't decode byte 0xc3 in position 5: ordinal not in range(128) Python:UnicodeDecodeError:&#39;ascii&#39;编解码器无法解码位置0的字节0xef:序数不在范围内(128) - Python: UnicodeDecodeError: 'ascii' codec can't decode byte 0xef in position 0: ordinal not in range(128) Python中的孟加拉语编码-UnicodeDecodeError:&#39;ascii&#39;编解码器无法解码位置0的字节0xe0:序数不在范围内(128) - bangla encoding in Python - UnicodeDecodeError: 'ascii' codec can't decode byte 0xe0 in position 0: ordinal not in range(128) Python 3 UnicodeDecodeError:&#39;ascii&#39;编解码器无法解码位置0中的字节0xe2:序数不在范围内(128) - Python 3 UnicodeDecodeError: 'ascii' codec can't decode byte 0xe2 in position 0: ordinal not in range(128)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM