使用Python2.7,我有一个端点,它返回包含字符“\u2019”、“\u2018”和“\u2026”的字符串。我还不能用任何编码、解码等组合来解决这些问题
实际字符串如下所示: “\u2018Ralph打破了互联网,2018年信条II是感恩节的热门歌曲”
这是一个代码片段
#!/usr/bin/python
# -*- coding: utf-8 -*-
...
>>> '\u2019'.encode('ascii')
'\\u2019'
>>> '\u2019'.encode('utf-8')
'\\u2019'
>>> '\u2019'.decode('utf-8')
u'\\u2019'
>>>'\u2019'.decode('ascii')
u'\\u2019'
我正在运行命令行,但也曾试图输出到文件,但没有用。在这类问题上有很多类似的线索,但是还没有找到一个能解决这个问题的。我想我可以做一些正则表达式字符查找和替换,但这看起来很笨拙。在
我已经投了@Ying Cai的票,但我会给你一些提示: 如果在使用},它应该可以像您预期的那样工作。在
Python 2.7
时添加from __future__ import unicode_literals
,则整个文件将被视为Python 3.X
,这意味着所有字符串文本都将被视为unicode。如果您在Python 2.7
上,并且使用u"\u2018Ralph Breaks the Internet\u2019 and \u2018Creed II\u2019 Are Thanksgiving Hits"
而不添加from __future__ import unicode_literals
,则字符串现在是{@Mark我刚刚更新了我的答案,因为我真的在想}。谢谢你的评论。在
from __future__ import unicode_literals
,而不是{你检查过这个线程了吗:Removing \u2018 and \u2019 character
这些是引号字符的Unicode。在
返回:
《拉尔夫打破互联网》和《信条2》是感恩节的热门歌曲
希望这有帮助。在
在Python2上打印非ASCII字符需要3件事。在
print
示例(使用代码页437的Windows控制台):
将代码页更改为支持以下字符的代码页:
^{pr2}$请注意,最新的python3的工作方式不同。代码页无关紧要(但字体重要):
相关问题 更多 >
编程相关推荐