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


Groups > comp.lang.python > #64653

Elementree and insert new element if it is not present

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 <tharanga.abeyseela@gmail.com>
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 <tharanga.abeyseela@gmail.com>
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 <python-list.python.org>
List-Unsubscribe <https://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list/>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <https://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.5925.1390527865.18130.python-list@python.org> (permalink)
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

Show key headers only | View raw


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

Hi,

I have the  following xml,a nd i need to add Description element if it is
not present.

<TVSeries><Provider>xxx</Provider><Title>X</Title><SortTitle>World's
Fastest Indian, The</SortTitle></TvSeries>
<Movies><Provider>xxx</Provider><Title>The World's Fastest
Indian</Title><Description> The World's Fastest Indian
</Description><SortTitle>World's Fastest Indian,
The</SortTitle></Movies>

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

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


Thread

Elementree and insert new element if it is not present Tharanga Abeyseela <tharanga.abeyseela@gmail.com> - 2014-01-24 12:44 +1100

csiph-web