On 26 August 2016 at 08:22, Frank Millman <fr...@chagford.com> wrote: > "Peter Otten" wrote in message news:npn25e$s5n$1...@blaine.gmane.org... > > Frank Millman wrote: > >>> As you have to keep the "<", why bother? >> >> >> If you mean why don't I convert the '<' to '<', the answer is that I do >> - I just omitted to say so. However, explicit is better than implicit :-) > > >> Doesn't that make the XML document invalid or changes it in an >> irreversible way? How would you know whether > > > "<foo><bar/></foo>" > > started out as > > "<foo><bar/></foo>" > > or > > "<foo><bar/></foo>" > > ? > > I cheat ;-) > > It is *my* XML, and I know that I only use the offending characters inside > attributes, and attributes are the only place where double-quote marks are > allowed. > > So this is my conversion routine - > > lines = string.split('"') # split on attributes > for pos, line in enumerate(lines): > if pos%2: # every 2nd line is an attribute > lines[pos] = line.replace('<', '<').replace('>', '>') > return '"'.join(lines) > > Frank > > > -- > https://mail.python.org/mailman/listinfo/python-list
or.. you could just escape all & as & before escaping the > and <, and do the reverse on decode -- https://mail.python.org/mailman/listinfo/python-list