How to encode html and xml tag datas with standard python modules ?

Fredrik Lundh fredrik at pythonware.com
Wed May 3 04:10:08 EDT 2006


DurumDara wrote:

> Have the python standard mod. lib. a html/xml encoder functions/procedures ?

you can use

    cgi.escape(s) # escapes < > &

for CDATA sections, and

    cgi.escape(s, True) # escapes < > & "

for attributes.

to output ASCII, use

    cgi.escape(s).encode("ascii", "xmlcharrefreplace")
    cgi.escape(s, True).encode("ascii", "xmlcharrefreplace")

>             if b<32 or b>127 or c in ['<','>','"',';','&','@','%','#']:
>                 c="&#%03d;"%b

that's a rather odd set of reserved characters...

</F>






More information about the Python-list mailing list