2012-03-08 96 views
5

我想在python中转换特殊字符,如"%$!&@á é ©",而不仅仅是'<&">',因为我发现的所有文档和参考资料都显示出来。 cgi.escape不能解决问题。如何将特殊字符转换为html实体?

例如,字符串"á ê ĩ &"应该转换为"&aacute; &ecirc; &itilde; &amp;"

是否anyboy知道如何解决它? 我正在使用python 2.6。

+2

请注意以下两点:(1)名称实体可能会导致问题,您应该使用数字实体。 (2)为什么要使用实体?在大多数情况下,更好的解决方案是对文档进行UTF-8编码,以便它可以包含字母,而不是使用实体。 – 2012-03-08 11:30:50

+1

http://wiki.python.org/moin/EscapingHtml – Quentin 2012-03-08 11:32:05

+0

我同意你@KonradRudolph。我不喜欢使用实体,但我正在使用的系统使用实体,所以我别无选择。 =/ – 2012-03-08 11:35:12

回答

7

你可以建立一个使用字典自己的循环,你可以找到在http://docs.python.org/library/htmllib.html#module-htmlentitydefs

你要找的人是htmlentitydefs.codepoint2name

+0

这是一个好主意! ; D – 2012-03-08 11:35:47

+0

链接不再有效。在Python 2中使用HTMLParser,在Python 3中使用等效的html.parser。 – oxidworks 2017-02-21 22:39:13

5

我发现了一个建在溶液中搜索的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 

谢谢大家的帮助! ;)

相关问题