Python 使用str.decode和unicode.encode的正确方法是什么?

Python 使用str.decode和unicode.encode的正确方法是什么?,python,Python,使用str.decode和unicode.encode的正确方法是什么 例如 Ignacio的示例是正确的,但这取决于您的控制台是否能够显示Unicode字符,而在Windows上通常无法显示Unicode字符。这里有同样的事情,只有安全字符串转义报告: >>> '\xe3\x81\x82'.decode('utf-8') # three top-bit-set bytes, representing one character u'\u3042'

使用str.decode和unicode.encode的正确方法是什么

例如


Ignacio的示例是正确的,但这取决于您的控制台是否能够显示Unicode字符,而在Windows上通常无法显示Unicode字符。这里有同样的事情,只有安全字符串转义报告:

>>> '\xe3\x81\x82'.decode('utf-8')    # three top-bit-set bytes, representing one character
u'\u3042'                             # Hiragana letter A

>>> u'\u3042'.encode('shift-jis')
'\x82\xa0'                            # only requires two bytes in the Shift-JIS encoding

>>> unicode('\x82\xa0', 'shift-jis')  # alternative way of doing a decode
u'\u3042'

当您写入文件或通过web服务器,或者您在控制台支持UTF-8的另一个操作系统上时,这会更容易一些。

文件D:\zjm_code\a.py,第4行语法错误:文件D:\zjm_code\a.py中的非ASCII字符“\xe3”在第4行,但未声明编码;有关详细信息,请参阅读取该URL并修复源代码。@zjm1126:作为第一行插入:编码:utf-8回溯最近的调用上次:文件D:\zjm_code\a.py,第5行,在打印“\xe3\x81\x82”中,解码“utf-8”UnicodeEncodeError:“ascii”编解码器无法对位置0中的字符u'\u3042'进行编码:序号不在range128@zjm1126:如前所述,请使用print reprsome_unicode而不是print某些_unicode。。。Windows标准不符合unicode-1:应该发布在pleasedomyhomeworkforme.com上谁能给你代码?你想要代码还是要写代码的人的名字?没有理由在类中调用它们,因为它们似乎不太容易在实例中调用。
>>> unicode.encode(u"abcd","utf8")
'abcd' #unicode string u"abcd" got encoded to UTF-8 encoded string "abcd"

>>> str.decode("abcd","utf8")
u'abcd' #UTF-8 string "abcd" got decoded to python's unicode object u"abcd"
>>>
>>> '\xe3\x81\x82'.decode('utf-8')    # three top-bit-set bytes, representing one character
u'\u3042'                             # Hiragana letter A

>>> u'\u3042'.encode('shift-jis')
'\x82\xa0'                            # only requires two bytes in the Shift-JIS encoding

>>> unicode('\x82\xa0', 'shift-jis')  # alternative way of doing a decode
u'\u3042'
>>> unicode.encode(u"abcd","utf8")
'abcd' #unicode string u"abcd" got encoded to UTF-8 encoded string "abcd"

>>> str.decode("abcd","utf8")
u'abcd' #UTF-8 string "abcd" got decoded to python's unicode object u"abcd"
>>>