Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #44496 > unrolled thread

Re: xml.etree.ElementTree if element does not exist?

Started byOmbongi Moraa Fe <moraa.lovetakes2@gmail.com>
First post2013-04-29 14:50 +0300
Last post2013-04-29 14:50 +0300
Articles 1 — 1 participant

Back to article view | Back to comp.lang.python

This discussion starts older than the indexed window; earlier articles aren't shown. The article labeled Started by below is the oldest one visible, not the original post.


Contents

  Re: xml.etree.ElementTree if element does not exist? Ombongi Moraa Fe <moraa.lovetakes2@gmail.com> - 2013-04-29 14:50 +0300

#44496 — Re: xml.etree.ElementTree if element does not exist?

FromOmbongi Moraa Fe <moraa.lovetakes2@gmail.com>
Date2013-04-29 14:50 +0300
SubjectRe: xml.etree.ElementTree if element does not exist?
Message-ID<mailman.1152.1367236624.3114.python-list@python.org>

[Multipart message — attachments visible in raw view] — view raw

Hi Stefan, Group

Thanks for the thumbs up.

Worked perfectly.

Saludos

Ombongi Moraa Faith


On 29 April 2013 14:22, Stefan Holdermans <stefan@vectorfabrics.com> wrote:

> Ombongi,
>
> > however, if i pass xml data that DOES NOT contain sepid element, i get
> an error:
> >
> > Traceback (most recent call last):
> >   File "/usr/local/bin/receive.py", line 21, in <module>
> >     sepid = content.find(".//{
> http://www.huawei.com.cn/schema/common/v2_1}sepid").text
> > AttributeError: 'NoneType' object has no attribute 'text'
> >
> >
> > some messages i receive will have the sepid parameter, other will not
> have this parameter. How can i cater for this? kinda like an if .. else
> implementation for xml.etree.ElementTree  ?
>
> What about simply testing whether the value returned by find is None? For
> example:
>
>   $ cat test.py
>   from xml.etree import ElementTree
>
>   myTree = ElementTree.fromstring('<test />')
>   myElement = myTree.find('orange')
>
>   if myElement is None:
>       print 'tree does not contain a child element "orange"'
>   else:
>       print myElement.text
>
>
>   $ python test.py
>   tree does not contain a child element "orange"
>
> HTH,
>
>   Stefan

[toc] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web