not quite 1252

Serge Orlov Serge.Orlov at gmail.com
Fri Apr 28 10:03:03 EDT 2006


Anton Vredegoor wrote:
> Anton Vredegoor wrote:
>
> > So, probably yes. If it doesn't have a text attribrute if you iterate
> > over it using OOopy for example:
>
> Sorry about that, I meant if the text attribute is None, but there *is*
> some text.

OK, I think I understand what you're talking about. It's ElementTree
API. I have a special generator to process content of elements:

def contents(e):
    """Yield sequence of subelements and text between subelements
    in the order as they come"""
    if e.text:
        yield e.text
    for sub_e in e.getchildren():
        yield sub_e
        if sub_e.tail:
            yield sub_e.tail

Example:

>>> doc = ET.fromstring("""<sometag>weather<space/>in Amsterdam<space/>is great</sometag>""")
>>> list(contents(doc))
['weather', <Element space at c8de68>, 'in Amsterdam', <Element space
at ca2120>, 'is great']




More information about the Python-list mailing list