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


Groups > comp.lang.python > #31471 > unrolled thread

Re: ElementTree Issue - Search and remove elements

Started byStefan Behnel <stefan_ml@behnel.de>
First post2012-10-17 08:00 +0200
Last post2012-10-17 08:00 +0200
Articles 1 — 1 participant

Back to article view | Back to comp.lang.python

This discussion starts older than the indexed window; earlier articles aren't shown. The article labeled Started by below is the oldest one visible, not the original post.


Contents

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

#31471 — Re: ElementTree Issue - Search and remove elements

FromStefan Behnel <stefan_ml@behnel.de>
Date2012-10-17 08:00 +0200
SubjectRe: ElementTree Issue - Search and remove elements
Message-ID<mailman.2325.1350453619.27098.python-list@python.org>
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

[toc] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web