Error when printing a variable in ipdb: UnicodeEncodeError: 'ascii' codec can't encode character '\u22f1' in position 314: ordinal not in range(12)
Error when printing a variable in ipdb: UnicodeEncodeError: 'ascii' codec can't encode character '\u22f1' in position 314: ordinal not in range(12)
我正在调试一个 Python 3 脚本,每次我尝试打印一个变量时,它都会给我以下错误:
ipdb> inputs
*** UnicodeEncodeError: 'ascii' codec can't encode character '\u22f1' in
position 314: ordinal not in range(12)
我尝试使用 sys.setdefaultencoding()
并在脚本顶部添加 # -*- coding: utf-8 -*-
将默认编码设置为 UTF-8,但两者均无效。
TL;DR: export LANG=C.UTF-8
@mike 解释说这是因为 Python 从启动它的环境中选择编码设置。如果找不到合适的编码,它会回退到默认值 'ascii'。
我的解决方案是按照 answer. Thus, I tried to set export LANG=en_US.UTF-8
and, to my surprise, it didn't work. However, when I tried another locale, export LANG=C.UTF-8
as suggested by another answer 中的说明更改 locale
,它奏效了。
我正在调试一个 Python 3 脚本,每次我尝试打印一个变量时,它都会给我以下错误:
ipdb> inputs
*** UnicodeEncodeError: 'ascii' codec can't encode character '\u22f1' in
position 314: ordinal not in range(12)
我尝试使用 sys.setdefaultencoding()
并在脚本顶部添加 # -*- coding: utf-8 -*-
将默认编码设置为 UTF-8,但两者均无效。
TL;DR: export LANG=C.UTF-8
@mike 解释说这是因为 Python 从启动它的环境中选择编码设置。如果找不到合适的编码,它会回退到默认值 'ascii'。
我的解决方案是按照 answer. Thus, I tried to set export LANG=en_US.UTF-8
and, to my surprise, it didn't work. However, when I tried another locale, export LANG=C.UTF-8
as suggested by another answer 中的说明更改 locale
,它奏效了。