如何显示中文单词而不是unicode单词 [英] how to show chinese word , not unicode word

查看:97
本文介绍了如何显示中文单词而不是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']

但是我想显示中文单词,所以我改变了这个:

but i want to show chinese word , so i change this:

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']

那么如何在我的代码中显示中文单词

so how to show chinese word in my code,

谢谢

推荐答案

默认情况下,打印较大的内置结构会给出每个元素的repr().如果要使用str()/unicode(),则需要自己遍历该序列.

By default, printing a larger built-in structure gives the repr() of each of the elements. If you want the str()/unicode() instead then you need to iterate over the sequence yourself.

a = u"['" + u"', '".join(token.text for token in ...) + u"']"
print a

这篇关于如何显示中文单词而不是unicode单词的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆