[英]Error when printing a variable in ipdb: UnicodeEncodeError: 'ascii' codec can't encode character '\u22f1' in position 314: ordinal not in range(12)
I was debugging a Python 3 script and every time I tried to print a variable, it gave me the following error: 我正在调试Python 3脚本,每次尝试打印变量时,都会出现以下错误:
ipdb> inputs
*** UnicodeEncodeError: 'ascii' codec can't encode character '\u22f1' in
position 314: ordinal not in range(12)
I tried to set the default encoding to UTF-8 using sys.setdefaultencoding()
and adding # -*- coding: utf-8 -*-
at the top of the script, but both didn't work. 我尝试使用
sys.setdefaultencoding()
将默认编码设置为UTF-8,并在脚本顶部添加# -*- coding: utf-8 -*-
,但是两者均无效。
TL;DR: export LANG=C.UTF-8
TL; DR:
export LANG=C.UTF-8
@mike explains that it happens because Python picks the encoding setting from the environment it's been initiated from. @mike解释说,发生这种情况是因为Python从其启动环境中选择了编码设置。 If it can't find a proper encoding, it falls back to its default, 'ascii'.
如果找不到正确的编码,则会恢复为默认值“ ascii”。
My solution was to change the locale
as described in this answer . 我的解决方案是按照此答案中的说明更改
locale
。 Thus, I tried to set export LANG=en_US.UTF-8
and, to my surprise, it didn't work. 因此,我尝试设置
export LANG=en_US.UTF-8
,令我惊讶的是,它没有用。 However, when I tried another locale, export LANG=C.UTF-8
as suggested by another answer , it worked. 但是,当我尝试其他语言环境时,按照另一个答案的建议
export LANG=C.UTF-8
,它可以工作。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.