Python打印中文报错(搜索看了不少,但貌似没讲解怎么解决这个的)
Python打印中文报错(搜索看了不少,但貌似没讲解如何解决这个的)
运行环境:win7 企业版英文OS, python 2.7.10(机器上也装有Python3.4).
打开命令行看到的是:Microsoft Windows [Version 6.1.7601]
Copyright (c) 2009 Microsoft Corporation. All rights reserved.
双击python.exe运行后print 中文没问题
只要是在命令行里面打Python进去输命令或者 python test.py这样运行的,打印中文都会报错:
C:\Work>chcp
Active code page: 936
C:\Work>python
Python 2.7.10 (default, May 23 2015, 09:40:32) [MSC v.1500 32 bit (Intel)] on wi
n32
Type "help", "copyright", "credits" or "license" for more information.
>>> print "中文"
?形?Traceback (most recent call last):
File "<stdin>", line 1, in <module>
IOError: [Errno 0] Error
>>> #encoding=gb2312
... print "中文"
?形?Traceback (most recent call last):
File "<stdin>", line 2, in <module>
IOError: [Errno 0] Error
>>> import sys
>>> type = sys.getfilesystemencoding()
>>> s = u'谷歌'
>>> print s.decode('gb2312').encode(type)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
UnicodeEncodeError: 'ascii' codec can't encode characters in position 0-1: ordin
al not in range(128)
>>> #!/usr/bin/env python
... #coding=utf-8
... s="中文"
>>>
>>> if isinstance(s, unicode):
... #s=u"中文"
... print s.encode('gb2312')
... else:
... #s="中文"
... print s.decode('utf-8').encode('gb2312')
...
Traceback (most recent call last):
File "<stdin>", line 6, in <module>
File "C:\Python27\lib\encodings\utf_8.py", line 16, in decode
return codecs.utf_8_decode(input, errors, True)
UnicodeDecodeError: 'utf8' codec can't decode byte 0xd6 in position 0: invalid c
ontinuation byte
以下获取的内容在idle和命令行打开后的是一样的:
>>> sys.getfilesystemencoding()
'mbcs'
>>> sys.getdefaultencoding()
'ascii'
求解决此问题
------解决思路----------------------
卸掉3.4重装2.7试试?
python文档有说
utf8字符串可能有标头Byte Order Mard,可能需要去掉
import codecs
if string[:3]==codecs.BOM_UTF8:
string = string[3:]
百度搜 BOM_UTF8 应该会有详细说明
------解决思路----------------------
print u'中文'
s=u'中文'
print s
这两句都没错(ubuntu,python2.7.6)
------解决思路----------------------
#conding=utf-8 #第一行
以上三行代码放处理中文的代码前面就可以了。
运行环境:win7 企业版英文OS, python 2.7.10(机器上也装有Python3.4).
打开命令行看到的是:Microsoft Windows [Version 6.1.7601]
Copyright (c) 2009 Microsoft Corporation. All rights reserved.
双击python.exe运行后print 中文没问题
只要是在命令行里面打Python进去输命令或者 python test.py这样运行的,打印中文都会报错:
C:\Work>chcp
Active code page: 936
C:\Work>python
Python 2.7.10 (default, May 23 2015, 09:40:32) [MSC v.1500 32 bit (Intel)] on wi
n32
Type "help", "copyright", "credits" or "license" for more information.
>>> print "中文"
?形?Traceback (most recent call last):
File "<stdin>", line 1, in <module>
IOError: [Errno 0] Error
>>> #encoding=gb2312
... print "中文"
?形?Traceback (most recent call last):
File "<stdin>", line 2, in <module>
IOError: [Errno 0] Error
>>> import sys
>>> type = sys.getfilesystemencoding()
>>> s = u'谷歌'
>>> print s.decode('gb2312').encode(type)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
UnicodeEncodeError: 'ascii' codec can't encode characters in position 0-1: ordin
al not in range(128)
>>> #!/usr/bin/env python
... #coding=utf-8
... s="中文"
>>>
>>> if isinstance(s, unicode):
... #s=u"中文"
... print s.encode('gb2312')
... else:
... #s="中文"
... print s.decode('utf-8').encode('gb2312')
...
Traceback (most recent call last):
File "<stdin>", line 6, in <module>
File "C:\Python27\lib\encodings\utf_8.py", line 16, in decode
return codecs.utf_8_decode(input, errors, True)
UnicodeDecodeError: 'utf8' codec can't decode byte 0xd6 in position 0: invalid c
ontinuation byte
以下获取的内容在idle和命令行打开后的是一样的:
>>> sys.getfilesystemencoding()
'mbcs'
>>> sys.getdefaultencoding()
'ascii'
求解决此问题
------解决思路----------------------
卸掉3.4重装2.7试试?
python文档有说
utf8字符串可能有标头Byte Order Mard,可能需要去掉
import codecs
if string[:3]==codecs.BOM_UTF8:
string = string[3:]
百度搜 BOM_UTF8 应该会有详细说明
------解决思路----------------------
print u'中文'
s=u'中文'
print s
这两句都没错(ubuntu,python2.7.6)
------解决思路----------------------
#conding=utf-8 #第一行
import sys
reload(sys)
sys.setdefaultencoding('utf8')
以上三行代码放处理中文的代码前面就可以了。