Search code examples
pythonunicodetornado

Encode unicode special character (symbol) in Python?


I am using Python (Tornado framework in specific) to develop a corp website. When I output the data to html, there were some special characters such as (r) or (tm) in unicode format, for example:

Google%u2122

How can I encode (or convert) it to the correct one, such as Google (TM). I try using encode() method but it didn't work.

Thank you very mych


Solution

  • Thanks Ikke for the link, as I am working in web environment (using Tornado as I mentioned) so the primary answer there didn't meet my requirement, instead this one 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')
    

    To apply to Tornado template, I create the f