1
c19 2013-11-26 15:41:54 +08:00
|
2
burgleaf OP Python 2.7.5 (default, May 15 2013, 22:44:16) [MSC v.1500 64 bit (AMD64)] on wi
32 Type "help", "copyright", "credits" or "license" for more information. >>> s = '(\xef\xbd\xa1\xef\xbd\xa5\xcf\x89\xef\xbd\xa5\xef\xbd\xa1)\xef\xbe\x89' >>> s1 = s.decode('utf-8') >>> print s1 Traceback (most recent call last): File "<stdin>", line 1, in <module> UnicodeEncodeError: 'gbk' codec can't encode character u'\uff61' in position 1: illegal multibyte sequence @c19 谢谢你。玩python时间不长,以上是我的输出,应该是和这个帖子一样的问题吧。应该怎么破? |
3
c19 2013-11-26 19:36:18 +08:00
@burgleaf ***见stackoverflow被采纳答案。***
print尝试把unicode编码成环境编码。你得环境编码是GBK。windows? Linux临时环境变量设置如下: c19@acer:~$ export LANG=zh_CN.GBK c19@acer:~$ python Python 2.7.5+ (default, Sep 19 2013, 13:48:49) [GCC 4.8.1] on linux2 Type "help", "copyright", "credits" or "license" for more information. >>> s = '(\xef\xbd\xa1\xef\xbd\xa5\xcf\x89\xef\xbd\xa5\xef\xbd\xa1)\xef\xbe\x89' >>> s1 = s.decode('utf-8') >>> s1 u'(\uff61\uff65\u03c9\uff65\uff61)\uff89' >>> print(s1) Traceback (most recent call last): File "<stdin>", line 1, in <module> UnicodeEncodeError: 'ascii' codec can't encode characters in position 1-5: ordinal not in range(128) >>> |
4
burgleaf OP @c19 灰常感谢!原因是pip安装python包会加载我的用户目录,我的用户目录恰好是中文的,ascii不能编码。解决办法是:
python目录 Python27\Lib\site-packages 建一个文件sitecustomize.py 内容写: import sys sys.setdefaultencoding('gb2312') python会自动运行这个文件。 |
5
pfe361c 2013-11-27 11:01:33 +08:00
mark
|
6
tanlianming 2013-12-17 16:37:10 +08:00
mark
|
7
stargazer 2014-08-26 14:37:20 +08:00
mark
|
8
hongfeiyu 2016-01-01 21:25:58 +08:00
赞赞赞,来到这我解决了心头大患
|