Unicode API响应引发错误“ascii”编解码器无法对22462位置的字符u“\u2019”进行编码

2024-04-26 23:10:30 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在进行一个API调用,响应包含unicode字符。将此响应加载到文件中会引发以下错误:

'ascii' codec can't encode character u'\u2019' in position 22462

我尝试过解码和编码的所有组合('utf-8')。在

代码如下:

^{pr2}$

很多开发者都面临这个问题。很多地方都要求使用.decode('utf-8'),或者在python的顶部使用# _*_ coding:utf-8 _*_。在

它仍然没有帮助。在

有人能帮我解决这个问题吗?在

以下是线索:

Traceback (most recent call last):
File "/Users/SM/PycharmProjects/zendesk/zendesk_tickets_api.py", line 102, in main
cleaned_data = json.loads(encoded_data)
File "/Users/SM/anaconda/lib/python2.7/json/__init__.py", line 339, in loads
return _default_decoder.decode(s)
File "/Users/SM/anaconda/lib/python2.7/json/decoder.py", line 364, in decode
obj, end = self.raw_decode(s, idx=_w(s, 0).end())
File "/Users/SM/anaconda/lib/python2.7/json/decoder.py", line 380, in raw_decode
obj, end = self.scan_once(s, idx)
ValueError: Invalid \escape: line 1 column 2826494 (char 2826493)

|********************************************************|
Invalid \escape: line 1 column 2826494 (char 2826493)

Tags: inpyjsondataliblineanacondausers
1条回答
网友
1楼 · 发布于 2024-04-26 23:10:30
inter = resp.text
string_e = inter#.decode('utf-8')
encoded_data = final.encode('utf-8')

text属性是一个Unicode字符串,它从原始字节解码,使用的是请求模块从HTTP报头猜测可能正在使用的任何编码。在

您可能不希望这样;JSON对于编码应该是什么有自己的想法,所以您应该让JSON解码器通过从resp.content获取原始响应字节并直接传递给json.loads来实现这一点。在

更重要的是,请求有一个快捷的方法来做同样的事情:resp.json()。在

^{pr2}$

尝试对JSON字符串文本格式的输入执行此操作是一个坏主意:您将错过一些有效的转义,而错误地取消转义其他转义。替换Unicode是对输入的实际错误。例如,考虑输入JSON:

{"message": "Open the file C:\\newfolder\\text.txt"}

更换后:

{"message": "Open the file C:\ ewfolder\ ext.txt"}

这显然不是有效的JSON。在

与其尝试对JSON编码的字符串进行操作,不如让json对输入进行解码,然后过滤结构化输出中的任何字符串。这可能涉及到使用递归函数深入到每个级别的数据,以查找要筛选的字符串。例如

def clean(data):
    if isinstance(data, basestring):
        return data.replace('\n', ' ').replace('\t', ' ').replace('\r', ' ')
    if isinstance(data, list):
        return [clean(item) for item in data]
    if isinstance(data, dict):
        return {clean(key): clean(value) for (key, value) in data.items()}
    return data

cleaned_data = clean(resp.json())

相关问题 更多 >