python3 中的reload(sys)和sys.setdefaultencoding('utf-8')

通常我们为了防止出现乱码会进行一下操作
import sys
reload(sys)
sys.setdefaultencoding(‘utf-8’)
但这是python2的写法,但是在python3中这个需要已经不存在了,这么做也不会什么实际意义。
如果你要这么做就会出现一下错误

sys.setdefaultencoding('utf-8')
AttributeError: module 'sys' has no attribute 'setdefaultencoding'
在Python2.x中由于str和byte之间没有明显区别,经常要依赖于defaultencoding来做转换。
在python3中有了明确的str和byte类型区别,从一种类型转换成另一种类型要显式指定encoding。

因此,以上语法可以改为

import importlib,sys
importlib.reload(sys)

猜你喜欢

转载自www.cnblogs.com/lxc1997ye/p/11665521.html