如何显示中文而非Unicode格式的字符
这是我的代码:
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']
那么我该怎么在我的代码中显示中文呢,
谢谢
1 个回答
3
默认情况下,打印一个较大的内置结构时,会显示每个元素的 repr()
结果。如果你想要显示 str()
或 unicode()
的结果,那你就需要自己逐个遍历这个序列。
a = u"['" + u"', '".join(token.text for token in ...) + u"']"
print a