简体   繁体   English

Python3中的“ UnicodeEncodeError:'ascii'编解码器无法编码字符”

[英]“UnicodeEncodeError: 'ascii' codec can't encode character” in Python3

I'm fetching JSON with Requests from an API (using Python 3.5) and when I'm trying to print (or use) the JSON, either by response.text, json.loads(...) or response.json(), I get an UnicodeEncodeError. 我正在从API(使用Python 3.5)中获取带有请求的JSON,而当我尝试通过response.text,json.loads(...)或response.json()打印(或使用)JSON时,我收到UnicodeEncodeError。

print(response.text)
UnicodeEncodeError: 'ascii' codec can't encode character '\xc5' in position 676: ordinal not in range(128)

The JSON contains an array of dictionaries with country names and some of them contain special characters, eg: (just one dictionary in the binary array for example) JSON包含带有国家名称的字典数组,其中一些包含特殊字符,例如:(例如,二进制数组中只有一个字典)

b'[{\n "name" : "\xc3\x85land Islands"\n}]

I have no idea why there is an encoding problem and also why "ascii" is used when Requests detects an UTF-8 encoding (and even by setting it manually to UTF-8 doesn't change anything). 我不知道为什么会有编码问题,为什么在请求检测到UTF-8编码时(甚至通过手动将其设置为UTF-8都不会改变)为何使用“ ascii”。

Edit2: The problem was Microsoft Visual Studio Code 1.4. Edit2:问题是Microsoft Visual Studio Code 1.4。 It wasn't able to print the characters. 它无法打印字符。

If your code is running within VS, then it sounds that Python can't work out the encoding of the inbuilt console, so defaults to ASCII. 如果您的代码在VS中运行,则听起来Python无法计算出内置控制台的编码,因此默认为ASCII。 If you try to print any non-ASCII then Python throws an error rather printing text that won't display. 如果您尝试打印任何非ASCII字词,则Python会抛出错误,而不是打印不会显示的文本。

You can force Python's encoding by using the PYTHONIOENCODING environment variable. 您可以使用PYTHONIOENCODING环境变量来强制使用Python的编码。 Set it within the run configuration for the script. 在脚本的运行配置中进行设置。

Depending on Visual Studio's console, you may get away with: 根据Visual Studio的控制台,您可能会逃脱:

PYTHONIOENCODING=utf-8

or you may have to use a typical 8bit charset like: 或者您可能必须使用典型的8位字符集,例如:

PYTHONIOENCODING=windows-1252

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 UnicodeEncodeError: 'ascii' codec can't encode character '\\xe9' - -when using urlib.request python3 - UnicodeEncodeError: 'ascii' codec can't encode character '\xe9' - -when using urlib.request python3 UnicodeEncodeError:'ascii'编解码器不能编码字符[...] - UnicodeEncodeError: 'ascii' codec can't encode character […] 收到UnicodeEncodeError的Python脚本:“ ascii”编解码器无法编码字符 - Python script receiving a UnicodeEncodeError: 'ascii' codec can't encode character Python错误:UnicodeEncodeError:'ascii'编解码器无法编码字符 - Python error : UnicodeEncodeError: 'ascii' codec can't encode character UnicodeEncodeError:'ascii'编解码器不能编码字符u'\\ xe4' - UnicodeEncodeError: 'ascii' codec can't encode character u'\xe4' UnicodeEncodeError:'ascii'编解码器不能编码字符u'\\ xef' - UnicodeEncodeError: 'ascii' codec can't encode character u'\xef' UnicodeEncodeError: 'ascii' 编解码器无法在打印功能中编码字符 - UnicodeEncodeError: 'ascii' codec can't encode character in print function PySpark — UnicodeEncodeError: 'ascii' 编解码器无法编码字符 - PySpark — UnicodeEncodeError: 'ascii' codec can't encode character UnicodeEncodeError:'ascii'编解码器无法编码字符u'\\ xe9' - UnicodeEncodeError: 'ascii' codec can't encode character u'\xe9' UnicodeEncodeError:'ascii'编解码器无法编码字符u'\\ xa3' - UnicodeEncodeError: 'ascii' codec can't encode character u'\xa3'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM