本文摘自php中文网,作者silencement,侵删。

当使用Python编程时,编码问题一直很让人头疼,程序中经常会碰到如下错误提示:
1 | UnicodeDecodeError: 'ascii' codec can't decode byte 0x?? in position 1: ordinal not in range(128)
|
这是由于python在安装时,默认的编码是ascii,当程序中出现非ascii编码时,python的处理常常会报上面的错误。
对于上面问题,一般有2种处理方法:
方法1:
在python代码开头加上如下代码块:
1 2 3 | import sys
reload(sys)
sys.setdefaultencoding('utf8')
|
这种方法是临时的,只在程序执行时生效,系统默认编码并没有改变。
阅读剩余部分
相关阅读 >>
Python的序列之列表的通用方法
怎么对numpy里数组元素赋统一的值
Python为什么有tcl
Python解释器怎么下载
字符串格式化 % vs format哪种更好
Python软件免费吗
Python实现获取外网ip并发邮件的方法
django使用locals() 函数的方法介绍
Python pip是什么原理
Python爬虫爬取视频的详细介绍
更多相关阅读请进入《Python》频道 >>
人民邮电出版社
python入门书籍,非常畅销,超高好评,python官方公认好书。
转载请注明出处:木庄网络博客 » python utf-8编码怎么设置