ElementTree: How to return only unicode?

Torsten Bronger bronger at physik.rwth-aachen.de
Sun Mar 15 11:26:11 CET 2009


Stefan Behnel writes:

> Torsten Bronger wrote:
>> Stefan Behnel writes:
>>> Torsten Bronger wrote:
>>>> [...]
>>>> My problem is that if there is only ASCII, these methods return
>>>> ordinary strings instead of unicode.  So sometimes I get str,
>>>> sometimes I get unicode.  Can one change this globally so that
>>>> they only return unicode?
> [...]
> I just looked at the code. It seems that you can use your own
> XMLTreeBuilder subclass and overwrite the "._fixtext()" method
> like this:
>     def _fixtext(self, text):
>         return text

Great.  Thus, the following monkeypatch seems to do the trick:

    from xml.etree import ElementTree
    # FixMe: Must go away with Python 3
    ElementTree.XMLTreeBuilder._fixtext = lambda self, text: text

Thank you!


Torsten Bronger, aquisgrana, europa vetus
                   Jabber ID: torsten.bronger at jabber.rwth-aachen.de

More information about the Python-list mailing list