当前位置: 首页 > 图灵资讯 > 行业资讯> sublime编译python脚本出错了怎么办

sublime编译python脚本出错了怎么办

发布时间:2025-01-08 14:39:07

原因分析:

(推荐教程:Python入门教程)

unicode中文是没有办法在sublime Text的console中输出的。

解决方法:

设置python默认字节流编/解码器按照utf8解码方式,把字节流编/解码为unicode。

在脚本中加入下面的代码即可:

importsys
reload(sys)
sys.setdefaultencoding("utf8")

作用:

在将字节流使用str()方法转换为str对象时,会调用默认的encode函数,如果没有上述系统的默认编码设置,则自动使用'ascii' codecs进行编码,对于非ascii编码的数据,比如utf8字节流会产生错误解码提示:

UnicodeEncodeError:'ascii'codeccan'tencodecharactersinposition0-5:ordinalnotinrange(128)

在utf8编码文件中写入汉字字符, 比如 s = '中文'时, 如果没有上述设置,运行程序会在初始s对象的值,报告错误解码提示:

UnicodeDecodeError:'ascii'codeccan'tdecodebyte0xe4inposition0:ordinalnotinrange(128)

相关文章

python3兼容python2吗

python3兼容python2吗

2025-05-09
python3 whl怎么安装

python3 whl怎么安装

2025-05-09
python 字典怎么提取value

python 字典怎么提取value

2025-05-09
python 怎样计算字符串的长度

python 怎样计算字符串的长度

2025-05-09
python 怎么样反向输出字符串

python 怎么样反向输出字符串

2025-05-09
python 怎么判断字符串开头

python 怎么判断字符串开头

2025-05-09