请求响应中的非ASCII字符

5
我使用requests从API源获取json数据。
req = requests.get('url')
context = json.loads(req.text)
print(context)

返回错误

UnicodeEncodeError at /view/
'ascii' codec can't encode characters in position 26018-26019: ordinal not in range(128)
Unicode error hint

The string that could not be encoded/decoded was: OLYURÉTHANE

我查看了req.text,并未发现非ASCII字符。这些字符似乎是在json.loads(..)之后出现的。


符号 É 不是 ASCII 符号。ASCII 字符列表 - Pavan
我知道如何忽略它或类似地排序。 - user4251615
你可以捕获并忽略异常,或检查字符串中每个字符的ord()是否小于128,如果你仍然需要处理文本,可以忽略这些字符。 - Pavan
2个回答

2

试试这个:

request_txt = req.text.encode('utf-8')
context = json.loads(request_txt)

你需要对出现错误的字符串应用.encode('utf-8')


1
尝试像@Maninder所写的类似方法,但是使用decode()而不是encode()
context = json.loads(req.text.decode("utf8"))

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接