python报错UnicodeEncodeError: ‘ascii’ codec can’t encode characters in position 0-1: ordinal not in range

2019-12-13 11:09 阅读 941 views 次 python报错UnicodeEncodeError: ‘ascii’ codec can’t encode characters in position 0-1: ordinal not in range已关闭评论

1.在命令行修改,仅本会话有效:
1)通过>>>sys.getdefaultencoding()查看当前编码(若报错,先执行>>>import sys >>>reload(sys));
2)通过>>>sys.setdefaultencoding('utf8')设置编码

1)在程序文件中以下三句
import sys
reload(sys)
sys.setdefaultencoding('utf8')

3.修改Python本环境(在我这不起作用)
在Python的Lib\site-packages文件夹下新建一个sitecustomize.py文件,内容为:
#coding=utf8
import sys
reload(sys)
sys.setdefaultencoding('utf8')

forky@forkyUbu:~$ python
Python 2.7.15+ (default, Oct 7 2019, 17:39:04)
[GCC 7.4.0] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import sys
>>> sys.path
['', '/usr/lib/python2.7', '/usr/lib/python2.7/plat-x86_64-linux-gnu', '/usr/lib/python2.7/lib-tk', '/usr/lib/python2.7/lib-old', '/usr/lib/python2.7/lib-dynload', '/home/forky/.local/lib/python2.7/site-packages', '/usr/local/lib/python2.7/dist-packages', '/usr/lib/python2.7/dist-packages']
>>>
>>>
forky@forkyUbu:~$ cd /home/forky/.local/lib/python2.7/site-packages
forky@forkyUbu:~/.local/lib/python2.7/site-packages$ ls
hgdemandimport hgext hgext3rd mercurial mercurial-5.2.egg-info
forky@forkyUbu:~/.local/lib/python2.7/site-packages$ vi sitecustomize.py

版权声明:本文著作权归原作者所有,欢迎分享本文,谢谢支持!
转载请注明:python报错UnicodeEncodeError: ‘ascii’ codec can’t encode characters in position 0-1: ordinal not in range | 蚂蚁浏览器文档
分类:电脑技术 标签:

评论已关闭!