如何使用 python 在我的代码中打印中文单词

发布于 2024-08-29 23:42:39 字数 1404 浏览 5 评论 0原文

这是我的代码:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

深居我梦 2024-09-05 23:42:39

您首先需要声明一个编码,因为错误消息说得很清楚——它甚至告诉您查看

顺便说一句,如果您的 sys.stdout 有一个 encoding,那么这样做会更简单(使用相同的编码声明)

print u'哈哈'.encode('utf-8')

,您甚至可能不需要 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

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).

雪花飘飘的天空 2024-09-05 23:42:39

需要指定python源代码文件的编码,这里是utf-8的编码。它位于 python 解释器路径下方的右上角。

#!/usr/bin/python
# -*- coding: utf-8 -*-

如果您转到错误消息中的网址 您可以找到有关指定 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.

#!/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.

深府石板幽径 2024-09-05 23:42:39

您无法对 unicode 字符进行编码。 Encode 用于将所有以 unicode 编码的字符转换为其他代码样式。它不能用于 unicode 字符。

在争议中,decode只能用于将非unicode编码的字符转换为unicode字符。

如果在字符串之前声明一个带有“u”字符的字符串,您将得到一个以 unicode 编码的字符串。您可以使用 isinstance(str, unicode) 来检测 str 是否以 unicode 编码。

尝试下面的代码。提示:在中文版本的Windows中,默认的代码风格是“gbk”。

>>> a = '哈哈'
>>>>> b = u'哈哈'
>>>>> isinstance(a,unicode)
错误
>>>>> isinstance(b,unicode)
正确

>>>一个
'\xb9\xfe\xb9\xfe'
>>>>> b
你'\u54c8\u54c8'

>>> a.decode('gbk')
你'\u54c8\u54c8'
>>>>> a_unicode = a.decode('gbk')
>>>>> a_unicode
你'\u54c8\u54c8'

>>>打印a_unicode
哈哈
>>>>> a_unicode.encode('gbk') == a
真实
>>>>> a_unicode == b
正确

>>> a.encode('gbk')
回溯(最近一次调用最后一次):
文件“”,第 1 行,位于
UnicodeDecodeError:“ascii”编解码器无法解码位置 0 中的字节 0xb9:序数不在范围内(128)

>>> b.decode('gbk')
回溯(最近一次调用最后一次):
文件“”,第 1 行,位于
UnicodeEncodeError:“ascii”编解码器无法对位置 0-1 中的字符进行编码:序数不在范围内(128)

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)

晒暮凉 2024-09-05 23:42:39

以下代码对我有用:

# coding: utf8
print u'哈哈'.encode('utf-8')

#coding 注释告诉 Python 文件本身的编码,因此您可以直接在其中嵌入 UTF-8 字符。如果您从 Unicode 字符串开始,则无需对其进行解码和重新编码。

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.

后知后觉 2024-09-05 23:42:39

根据威尔·麦卡琴的回答,这也有效:

# coding: utf8
print '哈哈'

Based off of Will McCutchen's answer, this also works:

# coding: utf8
print '哈哈'
末骤雨初歇 2024-09-05 23:42:39

您应该检查终端字符编码。

在我的终端上,首先我将字符编码设置为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 '鍝堝搱'.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文