XML Processing

Jay Loden python at jayloden.com
Thu Aug 2 21:06:52 CEST 2007


Robert Dailey wrote:
> Both strings in your example are exactly the same, unless I'm missing
> something.
> 
> On 8/2/07, Roman <rgelfand2 at hotmail.com> wrote:
>> Is there a package that converts a string that contains special
>> characters in xml to to literal value.  For instance, converts string
>> http://myhome/&param to http://myhome/&amp;param.
>>
>> Thanks in advance
>>
>> --
>> http://mail.python.org/mailman/listinfo/python-list


Robert, your newsreader/mail program is converting the HTML/XML entity codes into the corresponding character so you're not seeing the plain text version of what the OP posted. He's asking for code to convert XML entities like '&' into their escaped strings. 

I believe xml.sax.saxutils.encode() is what the OP is looking for:

>>> import xml.sax.saxutils
>>> xml.sax.saxutils.escape('http://myhome/&param')
'http://myhome/&amp;param'

-Jay



More information about the Python-list mailing list