Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!feeder.erje.net!eu.feeder.erje.net!xlned.com!feeder5.xlned.com!newsfeed.xs4all.nl!newsfeed2a.news.xs4all.nl!xs4all!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.008 X-Spam-Evidence: '*H*': 0.98; '*S*': 0.00; 'subject:not': 0.03; 'root': 0.05; 'skip:\xa0 30': 0.05; 'element': 0.07; 'none:': 0.07; 'except:': 0.09; 'try:': 0.09; 'def': 0.12; 'exist.': 0.16; 'statement.': 0.16; 'skip:\xa0 20': 0.24; "world's": 0.24; 'pass': 0.26; 'fastest': 0.30; 'message-id:@mail.gmail.com': 0.30; 'skip:( 20': 0.30; "i'm": 0.30; 'url:06': 0.31; 'checking': 0.33; 'skip:& 30': 0.33; 'problem': 0.35; 'but': 0.35; 'received:google.com': 0.35; 'add': 0.35; 'doing': 0.36; 'hi,': 0.36; 'wrong': 0.37; 'url:microsoft': 0.37; 'subject:new': 0.38; 'skip:& 10': 0.38; 'to:addr:python-list': 0.38; 'url:schemas': 0.38; 'skip:& 20': 0.39; 'indian': 0.39; 'to:addr:python.org': 0.39; 'skip:n 10': 0.64; 'here': 0.66; '8bit%:100': 0.72; 'present.': 0.74; 'url:2011': 0.75 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:date:message-id:subject:from:to:content-type; bh=Bvat97aTwlOCCPBttcazcYcbXLDVDtsMVwVAIkKFW9E=; b=HjC0N2ohnhleAG4msGHFSdVpG0u+DzCcxwqoqV8iEYiZKQxf4QKQZpyLRCac/maKYi XslQ7X4yhjGAJrmglyLxQgEd9mZScA3xjGC4HKYGkAhaCktCn0e2bStYlSkxZcet9jRi pAYC65mVEjRoxMN9xQO02MklpOdKJ96eJLT56nS++qu2wyY7fdqgjF7g6sLlyRh0AI/q TnUpnvI3HWM3s3TPZ+eMyvaXd78+Y+Ytubg2jZltL1eFd0OaZLkJjRRUeYE3NvqegK/X zk6GSnHAgeVl1aY016mEpm/OavsC9u8/VQ6/jtdustGIvSwJfLqCw8nl4NI5HJCBI8lk ABdA== MIME-Version: 1.0 X-Received: by 10.180.211.68 with SMTP id na4mr1413168wic.0.1390527864606; Thu, 23 Jan 2014 17:44:24 -0800 (PST) Date: Fri, 24 Jan 2014 12:44:24 +1100 Subject: Elementree and insert new element if it is not present From: Tharanga Abeyseela To: python-list@python.org Content-Type: multipart/alternative; boundary=001a11c344a26b959f04f0ad7fdb 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: 101 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1390527865 news.xs4all.nl 2956 [2001:888:2000:d::a6]:55335 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:64653 --001a11c344a26b959f04f0ad7fdb Content-Type: text/plain; charset=ISO-8859-1 Hi, I have the following xml,a nd i need to add Description element if it is not present. xxxXWorld's Fastest Indian, The xxxThe World's Fastest Indian The World's Fastest Indian World's Fastest Indian, The here is my function. def insert_description(root,type): for child in root.findall('.//{ http://schemas.microsoft.com/xxx/2011/06/13/ingest}%s' % type): title=child.find('.//{ http://schemas.microsoft.com/xxx/2011/06/13/ingestion}Title').text try: if child.find('Description') is None: new_desc = ET.Element('Description') new_desc.text = title child.insert(1, new_desc) except: pass root is the xm lfile, type includes (movies,shows,dramas etc) But this will add the description without checking the "Description" is exist. i have a problem with the following statement. (i guess) if child.find('Description') is None: what i'm doing wrong here, appreciate your help Regards, Tharanga --001a11c344a26b959f04f0ad7fdb Content-Type: text/html; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable
Hi,

I have the=A0 following xml,a nd i n= eed to add Description element if it is not present.

<TVSeries>
<Provider>xxx<=
span class=3D""></Provider>
<Title>X</Title>
<SortTitle>World's F=
astest Indian, The</SortTitle>
</TvSeries>

<Movies>
<Provider>xxx<=
span class=3D""></Provider>
<Title>The World'=
s Fastest Indian</Title>
<Description> The Wor=
ld's Fastest Indian </Description>=

<SortTitle>World'=
s Fastest Indian, The</SortTitle>
</Movies>

here is my function.

def= insert_description(root,type):
=A0=A0=A0=A0=A0=A0=A0 for child in root.= findall('.//{http://schemas.microsoft.com/xxx/2011/06/13/ingest}%s' % ty= pe):
=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0 title=3Dchild.find('.//{<= a href=3D"http://schemas.microsoft.com/xxx/2011/06/13/ingestion}Title')= .text">http://schemas.microsoft.com/xxx/2011/06/13/ingestion}Title').te= xt
=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0 try:
=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0 if ch= ild.find('Description') is None:
=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0= =A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0 new_desc = =3D ET.Element('Description')
=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0= =A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0 new_desc.text = =3D title
=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0= =A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0 child.insert(1, new_desc)

=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0 except:
=A0=A0=A0=A0= =A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0 pass

root is the xm lfile, type includes (movies,shows,dramas etc)

=
But this will add the description without checking the "Des= cription" is exist. i have a problem with the following statement. (i = guess)

if child.find('Description') is None:

what i&= #39;m doing wrong here, appreciate your help

Regards,
=
Tharanga
--001a11c344a26b959f04f0ad7fdb--