wie man ein chinesisches Wort in meinem Code ausgibt .. mit Python
Dies ist mein Code:
print '哈哈'.decode('gb2312').encode('utf-8')
... und es wird gedruckt:
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
Wie drucke ich '哈哈'?
Aktualisieren Wenn ich den folgenden Code verwende:
#!/usr/bin/python
# -*- coding: utf-8 -*-
print '哈哈'
... es druckt鍝堝搱
. Das wollte ich nicht bekommen.
Meine IDE ist Ulipad, ist das ein Fehler mit der IDE?
Zweites Update:
Dieser Code druckt die Zeichen richtig aus:
#!/usr/bin/python
# -*- coding: utf-8 -*-
print u'哈哈'.encode('gb2312')
... und wenn ich das benutze:
#!/usr/bin/python
# -*- coding: utf-8 -*-
a='哈哈'
print a.encode('gb2312')
Traceback (most recent call last):
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)
...oder..
#!/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)
... es funktioniert nicht. Wie würde ich die Variable @ drucka
passend
Vielen Dan