如何使用 python 在我的代码中打印中文单词
这是我的代码:
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
How do I print 'Ha'?
更新:当我使用以下代码时:
#!/usr/bin/python
# -*- coding: utf-8 -*-
print '哈哈'
...它打印鍝臂搱
。那不是我想要得到的。
我的IDE是Ulipad,这是IDE的错误吗?
第二次更新:
此代码将打印正确的字符:
#!/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)
...或...
#!/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
?
谢谢
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 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)
...it doesn't work. How would I print the variable a
appropriately?
thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
您首先需要声明一个编码,因为错误消息说得很清楚——它甚至告诉您查看
顺便说一句,如果您的
sys.stdout
有一个encoding,那么这样做会更简单(使用相同的编码声明)
,您甚至可能不需要
encode
部分正确设置 属性(取决于您的终端、操作系统等)。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
and you may not even need the
encode
part, if yoursys.stdout
has anencoding
attribute properly set (depends on your terminal, OS, etc).需要指定python源代码文件的编码,这里是utf-8的编码。它位于 python 解释器路径下方的右上角。
如果您转到错误消息中的网址 您可以找到有关指定 python 源文件编码的更多信息。
一旦指定了源文件的编码,您就不必解码文本。
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.
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.
您无法对 unicode 字符进行编码。 Encode 用于将所有以 unicode 编码的字符转换为其他代码样式。它不能用于 unicode 字符。
在争议中,decode只能用于将非unicode编码的字符转换为unicode字符。
如果在字符串之前声明一个带有“u”字符的字符串,您将得到一个以 unicode 编码的字符串。您可以使用 isinstance(str, unicode) 来检测 str 是否以 unicode 编码。
尝试下面的代码。提示:在中文版本的Windows中,默认的代码风格是“gbk”。
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".
以下代码对我有用:
#coding
注释告诉 Python 文件本身的编码,因此您可以直接在其中嵌入 UTF-8 字符。如果您从 Unicode 字符串开始,则无需对其进行解码和重新编码。The following code works for me:
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:
您应该检查终端字符编码。
在我的终端上,首先我将字符编码设置为utf-8,一切正常。
当我设置为GBK时,结果是“鍝臂搱”。
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 '鍝堝搱'.