编码ASCII文本

2024-10-01 09:28:02 发布

您现在位置:Python中文网/ 问答频道 /正文

我试着编码,但没有成功。你知道吗

text = "don\\u2019t think"
textencode = text.encode('utf-8').split(" ")
print textencode

结果仍然是['don\u2019t','think']

我试着得到['不要','思考']

有什么建议吗?你知道吗


Tags: text编码建议utfencodesplitprintdon
2条回答

在Python2.x中

>>> text = u"don\u2019t think"
>>> textencode = text.encode('utf-8').split(" ")
>>> print textencode
['don\xe2\x80\x99t', 'think']
>>> print textencode[0]
don’t

在双引号前加前缀“u”。你知道吗

看起来你在用Python2。这就是你要找的吗?你知道吗

>>> text = u"don\u2019t think"
>>> textencode = text.encode('utf-8').split(" ")
>>> print textencode[0]
don’t

Python3可以更好地处理unicode对象

>>> text = "don\u2019t think"
>>> textencode = text.split(" ")
>>> textencode
['don’t', 'think']

相关问题 更多 >