这是我的代码:
print '哈哈'.decode('gb2312').encode('utf-8')
...并且它会打印:
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
如何打印“哈哈”?
更新:当我使用以下代码时:
#!/usr/bin/python
# -*- coding: utf-8 -*-
print '哈哈'
...它打印了鍝堝搱
。这不是我想要的结果。
我的IDE是Ulipad,这是IDE的一个bug吗?
第二次更新:
这段代码将会正确地打印出字符:
#!/usr/bin/python
# -*- coding: utf-8 -*-
print u'哈哈'.encode('gb2312')
当我使用这个时:
#!/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)
...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)
...它不起作用。我应该如何适当地打印变量a
?
谢谢