在Python中编码unicode特殊字符(符号)?

时间:2012-02-29 09:46:03

标签: python unicode tornado

我正在使用Python(特定的Tornado框架)来开发一个公司网站。当我将数据输出到html时,有一些特殊字符,如(r)或(tm),采用unicode格式,例如:

谷歌%u2122

如何将其编码(或转换)为正确的,例如Google(TM)。我尝试使用encode()方法,但它不起作用。

非常感谢mych

1 个答案:

答案 0 :(得分:1)

感谢Ikke提供的链接,因为我在网络环境中工作(正如我所提到的那样使用Tornado)所以那里的主要答案不符合我的要求,而是这个https://stackoverflow.com/a/300556/183846

from urllib import unquote

def unquote_u(source):
    result = unquote(source)
    if '%u' in result:
        result = result.replace('%u','\\u').decode('unicode_escape')
    return result

print unquote_u('Tan%u0131m')

要应用于Tornado模板,我创建了f

相关问题