ElementTree surprise

Paul Rubin http
Thu Aug 16 00:46:29 EDT 2007


I have a doc with a bunch of fields like:

    <foo bar="spam">stuff</foo>
    <foo bar="penguin">other stuff</foo>

and sometimes

    <foo bar="parrot"></foo>

I use ElementTree to parse the doc and I use the .text attribute
to get "stuff" or "other stuff" in the spam and penguin examples.

I'd expect .text to be the empty string in the parrot example, but
instead it is None.

I can fix my script to deal with this, but it's surprising.  Is it
intentional?  I could understand it being None if the doc had said

   <foo bar="parrot"/>

but that is different.

Disclaimer: I'm not even slightly an XML expert, I just find myself
having to deal with a lot of it.



More information about the Python-list mailing list