python - 如何显示中文单词,而不是unicode单词

标签 python string list utf-8

这是我的代码:

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/

相关文章:

python - 在 GTK 中嵌入 chaco

python - Pandas :只保留累积变化超过阈值的每一行?

r - 将一些字符向量粘贴到 R 中的一个字符串中

html - 使用 CSS 向 HTML 列表添加边框线

python - 字符串拆分为字符,同时将其添加到元组

c - 交换链表项问题 C

python - Sentry 不显示 tastypie 错误消息

python - 如何在opencv中计算粗糙度轮廓?

java - 如何从中获取文本字符串

python - 每 n 个单词将字符串拆分为更小的字符串