', sin...">

How to convert special characters to html objects?

I want to convert special characters in python, such as "%$!&@á é ©"and not only '<&">', since all the documents and links that I have found so far show. cgi.escape does not solve the problem.

For example, the string "á ê ĩ &"should be converted to "&aacute; &ecirc; &itilde; &amp;".

Does anyone know how to solve it? I am using python 2.6.

+5
source share
2 answers

You can create your own loop using dictionaries, which you can find at http://docs.python.org/library/htmllib.html#module-htmlentitydefs

The one you are looking for is htmlentitydefs.codepoint2name

+7
source

htmlentitydefs.codepoint2name, @Ruben Vermeersch. : http://bytes.com/topic/python/answers/594350-convert-unicode-chars-html-entities

:

def htmlescape(text):
    text = (text).decode('utf-8')

    from htmlentitydefs import codepoint2name
    d = dict((unichr(code), u'&%s;' % name) for code,name in codepoint2name.iteritems() if code!=38) # exclude "&"    
    if u"&" in text:
        text = text.replace(u"&", u"&amp;")
    for key, value in d.iteritems():
        if key in text:
            text = text.replace(key, value)
    return text

!;)

+5

All Articles