我使用请求从 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(..)
最佳答案
试试这个:
request_txt = req.text.encode('utf-8')
context = json.loads(request_txt)
您需要将 .encode('utf-8')
应用于引发此错误的字符串。
关于python - 请求响应中的非 'ascii' 字符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44063290/