如何获得我的角色?

发布于 2024-12-29 03:38:03 字数 511 浏览 7 评论 0原文

我的系统:xp+python27 编解码器, xp gbk;python 27 ascii

>>> a = '你好'    
>>> a   
'\xc4\xe3\xba\xc3'  
>>> print a  
你好  
>>> '\xc4\xe3\xba\xc3'.decode('gbk')  
u'\u4f60\u597d'  
>>> '\xc4\xe3\xba\xc3'.encode('gbk')   
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
UnicodeDecodeError: 'ascii' codec can't decode byte 0xc4 in position 0: ordinal
not in range(128)   

如何从 '\xc4\xe3\xba\xc3' 得到“你好”?

my system:xp+python27
the codec,
xp gbk;python 27 ascii

>>> a = '你好'    
>>> a   
'\xc4\xe3\xba\xc3'  
>>> print a  
你好  
>>> '\xc4\xe3\xba\xc3'.decode('gbk')  
u'\u4f60\u597d'  
>>> '\xc4\xe3\xba\xc3'.encode('gbk')   
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
UnicodeDecodeError: 'ascii' codec can't decode byte 0xc4 in position 0: ordinal
not in range(128)   

How can I get "你好" from '\xc4\xe3\xba\xc3' ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

纸伞微斜 2025-01-05 03:38:03

这是可行的,因为您正在将字节解码为 un​​icode:

'\xc4\xe3\xba\xc3'.decode('gbk')

这不行,因为您正在尝试对字节进行编码(已经编码):

'\xc4\xe3\xba\xc3'.encode('gbk')

Python 2 中的错误消息在这里没有帮助,但您应该只在 unicode 上使用编码strings:

u'\u4f60\u597d'.encode('gbk')   # Gets you back the bytes you had before.

在 Python 2 中,只需在交互式提示符处执行 a 就会显示转义字符串中的非 ascii 字符(例如 \xc3\u4f60 )。您可以执行 print a 来显示字符。或者使用 Python 3,它将显示包含 unicode 字符的字符串。

This works, because you're decoding bytes to unicode:

'\xc4\xe3\xba\xc3'.decode('gbk')

This doesn't, because you're trying to encode bytes (which are already encoded):

'\xc4\xe3\xba\xc3'.encode('gbk')

The error message in Python 2 is unhelpful here, but you should only use encode on unicode strings:

u'\u4f60\u597d'.encode('gbk')   # Gets you back the bytes you had before.

In Python 2, just doing a at the interactive prompt will show non-ascii characters in strings escaped (like \xc3 or \u4f60). You can do print a to display the characters. Or use Python 3, which will display strings including the unicode characters.

紧拥背影 2025-01-05 03:38:03

您的 Python shell 无法打印 gbk 编码的字符串。它就在那里,只是你无法打印它。

Your Python shell can't print the gbk encoded string. It is there, you just can't print it.

无法回应 2025-01-05 03:38:03

他的意思是,在编码和打印时,它不会根据需要显示

>>> a = u'\u4f60\u597d'.encode('gbk')
>>> print a
���
>>> a
'\xc4\xe3\xba\xc3'

但如果指定:

>>> a = '\xe4\xbd\xa0\xe5\xa5\xbd'
>>> print a
你好

您应该使用:

>>> c = '\xe4\xbd\xa0\xe5\xa5\xbd'.decode('gbk')
>>> c
u'\u6d63\u72b2\u30bd'
>>> c = c.encode('gbk')
>>> print c
你好

he means that when encoding and printing it is not displaying as needed

>>> a = u'\u4f60\u597d'.encode('gbk')
>>> print a
���
>>> a
'\xc4\xe3\xba\xc3'

But if specifying:

>>> a = '\xe4\xbd\xa0\xe5\xa5\xbd'
>>> print a
你好

You should use:

>>> c = '\xe4\xbd\xa0\xe5\xa5\xbd'.decode('gbk')
>>> c
u'\u6d63\u72b2\u30bd'
>>> c = c.encode('gbk')
>>> print c
你好
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文