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


Groups > comp.lang.python > #44500

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

Path csiph.com!newsfeed.hal-mli.net!feeder3.hal-mli.net!newsfeed.hal-mli.net!feeder2.hal-mli.net!feeder.erje.net!eu.feeder.erje.net!newsfeed.fsmpi.rwth-aachen.de!news-1.dfn.de!news.dfn.de!news.informatik.hu-berlin.de!fu-berlin.de!uni-berlin.de!individual.net!not-for-mail
From Neil Cerutti <neilc@norwich.edu>
Newsgroups comp.lang.python
Subject Re: xml.etree.ElementTree if element does not exist?
Date 29 Apr 2013 12:29:52 GMT
Organization Norwich University
Lines 22
Message-ID <au77e0FhuuU3@mid.individual.net> (permalink)
References <mailman.1147.1367231260.3114.python-list@python.org> <au775tFhuuU1@mid.individual.net>
Mime-Version 1.0
Content-Type text/plain; charset=us-ascii
Content-Transfer-Encoding 7bit
X-Trace individual.net YfqxKX1D58H5QgYpI2oWPQeIXW0RKSp49ki02+OXhKYFFg2X6V
Cancel-Lock sha1:861DkstB0e3qHaOb9Lb4dMd2fCE=
User-Agent slrn/0.9.9p1/mm/ao (Win32)
Xref csiph.com comp.lang.python:44500

Show key headers only | View raw


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

Back to comp.lang.python | Previous | NextPrevious in thread | Find similar | Unroll thread


Thread

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

csiph-web