This is my code:
print '哈哈'.decode('gb2312').encode('utf-8')
...and it prints:
SyntaxError: Non-ASCII character '\xe5' in file D:\zjm_code\a.py on line 2, but no encoding declared; see http://www.python.org/peps/pep-0263.html for details
How do I print '哈哈'?
Update: When I use the following code:
#!/usr/bin/python
# -*- coding: utf-8 -*-
print '哈哈'
... it prints 鍝堝搱
. That isn't what I wanted to get.
My IDE is Ulipad, is this a bug with the IDE?
Second Update:
This code will print the characters right:
#!/usr/bin/python
# -*- coding: utf-8 -*-
print u'哈哈'.encode('gb2312')
...and when I use this:
#!/usr/bin/python
# -*- coding: utf-8 -*-
a='哈哈'
print a.encode('gb2312')
Traceback (most recent call las开发者_开发问答t):
File "D:\zjm_code\a.py", line 5, in <module>
print a.encode('gb2312')
UnicodeDecodeError: 'ascii' codec can't decode byte 0xe5 in position 0: ordinal not in range(128)
...or...
#!/usr/bin/python
# -*- coding: utf-8 -*-
a='哈哈'
print unicode(a).encode('gb2312')
Traceback (most recent call last):
File "D:\zjm_code\a.py", line 5, in <module>
print unicode(a).encode('gb2312')
UnicodeDecodeError: 'ascii' codec can't decode byte 0xe5 in position 0: ordinal not in range(128)
...it doesn't work. How would I print the variable a
appropriately?
thanks
You first need to declare an encoding, as the error messages says so clearly -- it even tells you to look here for details! Your encoding is presumably gb2312
.
BTW, it would be simpler (with the same encoding declaration) to do
print u'哈哈'.encode('utf-8')
and you may not even need the encode
part, if your sys.stdout
has an encoding
attribute properly set (depends on your terminal, OS, etc).
You need to specify the encoding of the python source code file, here is the coding for utf-8. It goes at the top right underneath the path the the python interpreter.
#!/usr/bin/python
# -*- coding: utf-8 -*-
If you go to the url in the error message you can find more information about specifying the encoding of a python source file.
Once you specify the encoding of the source file, you shouldn't have to decode the text.
You can't do encode on unicode character. Encode is used to translate all character encoded in unicode to other code style. It can't be used to unicode character.
In the controversy way, decode can only used to character not encoded in unicode to translate to unicode character.
If you declare a string with 'u' character before the string, you will get a string encoded in unicode. You can use isinstance(str, unicode) to detect if the str is encoded in unicode.
Try this code below. Hint: in Windows with Chinese version, default code style is "gbk".
>>> a = '哈哈'
>>> b = u'哈哈'
>>> isinstance(a,unicode)
False
>>> isinstance(b,unicode)
True>>> a
'\xb9\xfe\xb9\xfe'
>>> b
u'\u54c8\u54c8'>>> a.decode('gbk')
u'\u54c8\u54c8'
>>> a_unicode = a.decode('gbk')
>>> a_unicode
u'\u54c8\u54c8'>>> print a_unicode
哈哈
>>> a_unicode.encode('gbk') == a
True
>>> a_unicode == b
True>>> a.encode('gbk')
Traceback (most recent call last): File "", line 1, in UnicodeDecodeError: 'ascii' codec can't decode byte 0xb9 in position 0: ordinal not in range(128)>>> b.decode('gbk')
Traceback (most recent call last): File "", line 1, in UnicodeEncodeError: 'ascii' codec can't encode characters in position 0-1: ordinal not in range(128)
The following code works for me:
# coding: utf8
print u'哈哈'.encode('utf-8')
The #coding
comment tells Python the encoding of the file itself, so you can embed UTF-8 characters in it directly. And if you start from a Unicode string, there is no need to decode it and the re-encode it.
Based off of Will McCutchen's answer, this also works:
# coding: utf8
print '哈哈'
You should check you terminal character encoding.
On my terminal, first i set character encoding to utf-8, everything is alright.
When i set it to GBK, the result is '鍝堝搱'.
精彩评论