Regular Expressions: Can't quite figure this problem out

Gabriel Genellina gagsl-py2 at yahoo.com.ar
Mon Sep 24 22:08:12 EDT 2007


En Mon, 24 Sep 2007 22:53:57 -0300, Robert Dailey <rcdailey at gmail.com>  
escribi�:

> Any easier way to do a 'replace' then using start(), end(), and slicing
> operations? I'm currently doing it manually.

Yes: forget about regular expressions. ElementTree does that for free:

source = """
<xml>
<root></root>
<root/>
<root><frame type="image"><action></action></frame></root>
<root><frame type="image"><action/></frame></root>
</xml>
"""

import xml.etree.ElementTree as ET
tree = ET.XML(source)
print ET.tostring(tree)

output:

<xml>
<root />
<root />
<root><frame type="image"><action /></frame></root>
<root><frame type="image"><action /></frame></root>
</xml>

-- 
Gabriel Genellina




More information about the Python-list mailing list