python - 请求响应中的非 'ascii' 字符

标签 python python-3.x unicode python-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(..)

之后

最佳答案

试试这个:

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

您需要将 .encode('utf-8') 应用于引发此错误的字符串。

关于python - 请求响应中的非 'ascii' 字符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44063290/

相关文章:

Python 从另一个文件导出的包的子模块导入

Python 应用程序因 "PyEval_RestoreThread: the function must be called with the GIL held, but the GIL is released"崩溃

javascript - Django、Javascript、JSON 和 Unicode

django - 如何让 Django 从 unicode 字符创建 slug?

python - Flask 登录@login_required 不工作

python - 如何使用 selenium 在 Twitter 登录页面中单击下一步 - Python

python - 在图像上绘制多个透明蒙版

python - 将变量从 MQTT 传递给 Kivy

python - 根据条件将某些行的值与另一个最近的行相加

c# - 在 C# 控制台应用程序中显示阿拉伯字符