Path: csiph.com!usenet.pasdenom.info!gegeweb.org!newsfeed.kamp.net!newsfeed.kamp.net!newsfeed.freenet.ag!news2.euro.net!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.000 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'example:': 0.03; 'subject:not': 0.03; 'else:': 0.03; 'tree': 0.05; '21,': 0.07; 'attribute': 0.07; 'element': 0.07; 'error:': 0.07; 'none:': 0.07; 'cc:addr:python-list': 0.11; 'python': 0.11; 'element,': 0.16; 'parameter.': 0.16; 'stefan': 0.19; 'import': 0.22; 'cc:addr:python.org': 0.22; 'print': 0.22; 'this?': 0.23; 'cc:2**0': 0.24; 'cc:no real name:2**0': 0.24; 'pass': 0.26; 'skip:" 20': 0.27; 'header:In-Reply-To:1': 0.27; 'testing': 0.29; 'xml': 0.29; 'returned': 0.30; 'file': 0.32; '(most': 0.33; 'received:google.com': 0.35; 'subject:?': 0.36; 'recent': 0.39; 'does': 0.39; 'how': 0.40; 'simply': 0.61; 'header:Message-Id:1': 0.63; 'to:addr:gmail.com': 0.65; 'receive': 0.70; 'cater': 0.84; 'url:cn': 0.93 X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20120113; h=x-received:content-type:mime-version:subject:from:in-reply-to:date :cc:content-transfer-encoding:message-id:references:to:x-mailer :x-gm-message-state; bh=JwBI0Sjj9QJyJ0pxc54jjPuO3wbWdBUzXmked1Qz/H0=; b=jHclelr/A/JMDLGG301fmFj0qXxott8tJI7fS1GO9tSJwFmkWkFPTrF9PsWrIVH3Ws ZN6sA9GmtFRMPI/uyaxdQfhOC3rgJzqg9ZEoxenFdbh3ty7Vg2ya5VI8Qpha+3Fhy/6G 1gvaS+5t88mEqhwrA0sIrJf8FSVRzlvSDga+KFeUjLDfECVad9BRE6BZOJNPdYU7rf6a KrcvWEgsz5VavT2Ji4SF0n4Kml1XkCN/nNpDLg2ujcsH/LnEZ5qlad+ZaGnmVGN8RkgW wLKmlwZY5UnJRNHjXoqCOASoU04LP+olZ6WIVBxJ3fagNUwkYWft0mDdnNPK6sa/yxJI KmkA== X-Received: by 10.194.143.50 with SMTP id sb18mr95450828wjb.44.1367234554584; Mon, 29 Apr 2013 04:22:34 -0700 (PDT) Content-Type: text/plain; charset=iso-8859-1 Mime-Version: 1.0 (Mac OS X Mail 6.3 \(1503\)) Subject: Re: xml.etree.ElementTree if element does not exist? From: Stefan Holdermans In-Reply-To: Date: Mon, 29 Apr 2013 13:22:31 +0200 Content-Transfer-Encoding: quoted-printable References: To: Ombongi Moraa Fe X-Mailer: Apple Mail (2.1503) X-Gm-Message-State: ALoCoQkBvs6jVhSjemHqKTv/LH5iEH8ZkIaTIZcnFEe1dKXooHRZSREr7u0Id6M/NG6bGiK7TEJt Cc: python-list@python.org X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 38 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1367234562 news.xs4all.nl 16010 [2001:888:2000:d::a6]:43914 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:44494 Ombongi, > however, if i pass xml data that DOES NOT contain sepid element, i get = an error: >=20 > Traceback (most recent call last): > File "/usr/local/bin/receive.py", line 21, in > sepid =3D = content.find(".//{http://www.huawei.com.cn/schema/common/v2_1}sepid").text= > AttributeError: 'NoneType' object has no attribute 'text' >=20 >=20 > 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 =3D ElementTree.fromstring('') myElement =3D myTree.find('orange') if myElement is None: print 'tree does not contain a child element "orange"' else: print myElement.text =20 $ python test.py tree does not contain a child element "orange" HTH, Stefan=