Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #44491 > unrolled thread
| Started by | Ombongi Moraa Fe <moraa.lovetakes2@gmail.com> |
|---|---|
| First post | 2013-04-29 13:26 +0300 |
| Last post | 2013-04-29 12:29 +0000 |
| Articles | 3 — 2 participants |
Back to article view | Back to comp.lang.python
RE: xml.etree.ElementTree if element does not exist? Ombongi Moraa Fe <moraa.lovetakes2@gmail.com> - 2013-04-29 13:26 +0300
Re: xml.etree.ElementTree if element does not exist? Neil Cerutti <neilc@norwich.edu> - 2013-04-29 12:25 +0000
Re: xml.etree.ElementTree if element does not exist? Neil Cerutti <neilc@norwich.edu> - 2013-04-29 12:29 +0000
| From | Ombongi Moraa Fe <moraa.lovetakes2@gmail.com> |
|---|---|
| Date | 2013-04-29 13:26 +0300 |
| Subject | RE: xml.etree.ElementTree if element does not exist? |
| Message-ID | <mailman.1147.1367231260.3114.python-list@python.org> |
[Multipart message — attachments visible in raw view] — view raw
Good Afternoon,
Among other elements values that my script finds is value for sepid
sepid = content.find(".//{http://www.huawei.com.cn/schema/common/v2_1}sepid
").text
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 ?
Thanks in advance.
Saludos
Ombongi Moraa Faith
[toc] | [next] | [standalone]
| From | Neil Cerutti <neilc@norwich.edu> |
|---|---|
| Date | 2013-04-29 12:25 +0000 |
| Message-ID | <au775tFhuuU1@mid.individual.net> |
| In reply to | #44491 |
On 2013-04-29, Ombongi Moraa Fe <moraa.lovetakes2@gmail.com> wrote:
>
> Good Afternoon,
>
> Among other elements values that my script finds is value for sepid
>
> sepid = content.find(".//{http://www.huawei.com.cn/schema/common/v2_1}sepid
> ").text
>
>
> 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'
find returns None when it doesn't find what you asked for. So you
can't check the .text attribute right away unless you want an
exception thrown. I deal with these annoyances like this:
sepelem = content.find(".//{http://www.huawei.com.cn/schema/common/v2_1}sepid")
if sepelem is not None:
sepid = sepid.text
else:
sepid = ''
The empty string works for my purposes. Your script might need
something else.
--
Neil Cerutti
[toc] | [prev] | [next] | [standalone]
| From | Neil Cerutti <neilc@norwich.edu> |
|---|---|
| Date | 2013-04-29 12:29 +0000 |
| Message-ID | <au77e0FhuuU3@mid.individual.net> |
| In reply to | #44498 |
On 2013-04-29, Neil Cerutti <neilc@norwich.edu> wrote:
> find returns None when it doesn't find what you asked for. So you
> can't check the .text attribute right away unless you want an
> exception thrown. I deal with these annoyances like this:
>
> sepelem = content.find(".//{http://www.huawei.com.cn/schema/common/v2_1}sepid")
> if sepelem is not None:
> sepid = sepid.text
Oops. One edit too fiew. That line should of course be
sepid = sepelem.text
> else:
> sepid = ''
>
> The empty string works for my purposes. Your script might need
> something else.
--
Neil Cerutti
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.python
csiph-web