这是我的代码:
from whoosh.analysis import RegexAnalyzer
rex = RegexAnalyzer(re.compile(ur"([\u4e00-\u9fa5])|(\w+(\.?\w+)*)"))
a=[(token.text) for token in rex(u"hi 中 000 中文测试中文 there 3.141 big-time under_score")]
self.render_template('index.html',{'a':a})
它在网页上显示:
[u'hi', u'\u4e2d', u'000', u'\u4e2d', u'\u6587', u'\u6d4b', u'\u8bd5', u'\u4e2d', u'\u6587', u'there', u'3.141', u'big', u'time', u'under_score']
但是我想显示中文单词,所以我改变了这个:
a=[(token.text).encode('utf-8') for token in rex(u"hi 中 000 中文测试中文 there 3.141 big-time under_score")]
它显示:
['hi', '\xe4\xb8\xad', '000', '\xe4\xb8\xad', '\xe6\x96\x87', '\xe6\xb5\x8b', '\xe8\xaf\x95', '\xe4\xb8\xad', '\xe6\x96\x87', 'there', '3.141', 'big', 'time', 'under_score']
那么如何在我的代码中显示中文单词,
谢谢
最佳答案
默认情况下,打印较大的内置结构会给出每个元素的 repr()
。如果您想要 str()
/unicode()
,那么您需要自己迭代该序列。
a = u"['" + u"', '".join(token.text for token in ...) + u"']"
print a
关于python - 如何显示中文单词,而不是unicode单词,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3172741/