修改python编译器的默认编码格式

python在安装时,默认的编码是ascii,当程序中出现非ascii编码时,python的处理常常会报这样的错UnicodeDecodeError: 'ascii' codec can't decode byte 0x?? in position 1: ordinal not in range(128),python没办法处理非ascii编码的,此时需要自己设置将python的默认编码,一般设置为utf8的编码格式。

有三种方案:

方案一:(命令行,本会话有效)

1)通过>>>sys.getdefaultencoding()查看当前编码(若报错,先执行>>>import sys >>>reload(sys));

2)通过>>>sys.setdefaultencoding('utf8')设置编码

方案二:(较繁琐,最有效)

1)在程序文件中以下三句

import sys

reload(sys)

sys.setdefaultencoding('utf8')

方案三:(修改Python本环境永久有效,推荐)

在Python的Lib\site-packages文件夹下新建一个sitecustomize.py文件,内容为:

#coding=utf8

import sys

reload(sys)

sys.setdefaultencoding('utf8')

重启Python解释器,发现编码已被设置为utf8,与方案二同效;这是因为系统在Python启动的时候,自行调用该文件,设置系统的默认编码,而不需要每次都手动加上解决代码,属于一劳永逸的解决方法。


 sitecustomize.py is a special script; Python will try to import it on startup, so any code in it will be run automatically. As the comment mentions, it can go anywhere (as long as import can find it), but it usually goes in the site-packages directory within your Python lib directory.
 setdefaultencoding function sets, well, the default encoding. This is the encoding scheme that Python will try to use whenever it needs to auto-coerce a unicode string into a regular string.

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值