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


Groups > comp.lang.python > #31471

Re: ElementTree Issue - Search and remove elements

Path csiph.com!usenet.pasdenom.info!aioe.org!news.stack.nl!newsfeed.xs4all.nl!newsfeed6.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail
Return-Path <python-python-list@m.gmane.org>
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; 'root': 0.04; 'attribute': 0.05; 'context': 0.05; 'python': 0.09; 'ex:': 0.09; 'nc,': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'script,': 0.09; 'xml.': 0.09; 'archive': 0.11; 'thread': 0.11; 'from:addr:behnel.de': 0.16; 'from:addr:stefan_ml': 0.16; 'from:name:stefan behnel': 0.16; 'node,': 0.16; 'received:80.91.229.3': 0.16; 'received:plane.gmane.org': 0.16; 'subject:remove': 0.16; 'url:ccc': 0.16; 'string': 0.17; 'items.': 0.17; 'stefan': 0.17; 'subject:Issue': 0.17; 'shell': 0.18; '>>>': 0.18; '(not': 0.20; 'code.': 0.20; 'import': 0.21; 'error.': 0.21; 'elements': 0.23; 'header:In-Reply-To:1': 0.25; 'header:User- Agent:1': 0.26; '(most': 0.27; 'skip:# 10': 0.27; 'found.': 0.27; 'tree': 0.27; 'header:X-Complaints-To:1': 0.28; 'skip:( 20': 0.28; 'run': 0.28; 'node': 0.29; 'parent': 0.29; 'url:06': 0.29; 'code': 0.31; 'point': 0.31; 'file': 0.32; 'print': 0.32; 'getting': 0.33; 'traceback': 0.33; 'to:addr:python-list': 0.33; 'hi,': 0.33; 'false': 0.35; 'similar': 0.35; 'received:org': 0.36; 'but': 0.36; 'child': 0.36; 'should': 0.36; 'problems': 0.36; 'rather': 0.37; 'subject:: ': 0.38; 'object': 0.38; 'several': 0.39; 'to:addr:python.org': 0.39; 'url:schemas': 0.39; 'header:Received:5': 0.40; 'your': 0.60; 'remove': 0.61; 'helps': 0.63; 'details': 0.63; 'reply': 0.66; 'url:2011': 0.72; 'url:jpg': 0.84; '(they': 0.84; 'subject:Search': 0.84; 'rating': 0.93; 'responses': 0.93
X-Injected-Via-Gmane http://gmane.org/
To python-list@python.org
From Stefan Behnel <stefan_ml@behnel.de>
Subject Re: ElementTree Issue - Search and remove elements
Date Wed, 17 Oct 2012 08:00:00 +0200
References <CAOMTrffg175xYWwt0GBKXZQPKEYn1hMkbEYqzu8C2AL1EAaYoQ@mail.gmail.com>
Mime-Version 1.0
Content-Type text/plain; charset=UTF-8
Content-Transfer-Encoding 7bit
X-Gmane-NNTP-Posting-Host fw-snc-frn5-de01.fw.telefonica.de
User-Agent Mozilla/5.0 (X11; Linux x86_64; rv:15.0) Gecko/20120912 Thunderbird/15.0.1
In-Reply-To <CAOMTrffg175xYWwt0GBKXZQPKEYn1hMkbEYqzu8C2AL1EAaYoQ@mail.gmail.com>
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 <http://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 <http://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.2325.1350453619.27098.python-list@python.org> (permalink)
Lines 84
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1350453619 news.xs4all.nl 6868 [2001:888:2000:d::a6]:46161
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:31471

Show key headers only | View raw


Hi,

note that it's best to reply to responses you get, rather than starting a
new thread on the same topic. It helps in building up context and in
keeping details together at one point in the archive for users who run into
similar problems later.

Tharanga Abeyseela, 17.10.2012 07:47:
> I need to remove the parent node, if a particular match found.
> 
> ex:
> 
> <?xml version="1.0" encoding="ISO-8859-1" standalone="yes"?>
> <Feed xmlns="http://schemas.xxxx.xx/xx/2011/06/13/xx">
>     <TVEpisode>
>         <Provider>0x5</Provider>
>         <ItemId>http://fxxxxxxl</ItemId>
>         <Title>WWE</Title>
>         <SortTitle>WWE </SortTitle>
>         <Description>WWE</Description>
>         <IsUserGenerated>false</IsUserGenerated>
>         <Images>
>             <Image>
>                 <ImagePurpose>BoxArt</ImagePurpose>
>                 <Url>https://xxxxxx.xx/@006548-thumb.jpg</Url>
>             </Image>
>         </Images>
>         <LastModifiedDate>2012-10-16T00:00:19.814+11:00</LastModifiedDate>
>         <Genres>
>             <Genre>xxxxx</Genre>
>         </Genres>
>         <ParentalControl>
>             <System>xxxx</System>
>             <Rating>M</Rating>
> 
> 
> if i found <Rating>NC</Rating>, i need to remove the <TVEpisode> from
> the XML. i have TVseries,Movies,and several items. (they also have
> Rating element). i need to remove all if i found the NC keyword.inside
> <Ratging>
> 
> 
> im using following code.
> 
> when i do the following on python shell  i can see the result (NC,M,etc)
> 
> >>> x[1].text
> 'NC'
> 
> but when i do this inside the script, im getting the following error.
> 
> Traceback (most recent call last):
>   File "./test.py", line 10, in ?
>     x = child.find('Rating').text
> AttributeError: 'NoneType' object has no attribute 'text'
> 
> 
> but how should i remove the parent node if i found the string "NC" i
> need to do this for all elements (TVEpisode,Movies,TVshow etc)
> how can i use python to remove the parent node if that string found.
> (not only TVEpisodes, but others as well)
> 
> 
> #!/usr/bin/env python
> 
> import elementtree.ElementTree as ET
> 
> tree = ET.parse('test.xml')
> root = tree.getroot()
> 
> 
> for child in root.findall(".//{http://schemas.CCC.com/CCC/2011/06/13/CC}Rating"):
>        x = child.find('Rating').text
>         if child[1].text == 'NC':
>                 print "found"
>                root.remove('TVEpisode') ?????
> tree.write('output.xml')

The trick is to search for the parent node, then let your code find out if
you need to remove it or not by traversing its subtree.

Stefan

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


Thread

Re: ElementTree Issue - Search and remove elements Stefan Behnel <stefan_ml@behnel.de> - 2012-10-17 08:00 +0200

csiph-web