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


Groups > comp.lang.python > #31469

ElementTree Issue - Search and remove elements

Path csiph.com!usenet.pasdenom.info!weretis.net!feeder4.news.weretis.net!ecngs!feeder2.ecngs.de!newsfeed.freenet.ag!news2.euro.net!newsgate.cistron.nl!newsgate.news.xs4all.nl!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.004
X-Spam-Evidence '*H*': 0.99; '*S*': 0.00; 'root': 0.04; 'attribute': 0.05; 'python': 0.09; 'ex:': 0.09; 'nc,': 0.09; 'script,': 0.09; 'xml.': 0.09; 'guys,': 0.16; 'node,': 0.16; 'subject:remove': 0.16; 'url:ccc': 0.16; 'string': 0.17; 'items.': 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; '(most': 0.27; 'skip:# 10': 0.27; 'found.': 0.27; 'thoughts': 0.27; 'tree': 0.27; 'message-id:@mail.gmail.com': 0.27; 'skip:( 20': 0.28; 'node': 0.29; 'parent': 0.29; 'url:06': 0.29; 'this.': 0.29; 'file': 0.32; 'print': 0.32; 'getting': 0.33; 'traceback': 0.33; 'to:addr:python-list': 0.33; 'received:google.com': 0.34; 'thanks': 0.34; 'false': 0.35; 'received:209.85': 0.35; 'really': 0.36; 'but': 0.36; 'child': 0.36; 'should': 0.36; 'received:209': 0.37; '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; 'url:2011': 0.72; 'url:jpg': 0.84; '(they': 0.84; 'subject:Search': 0.84; 'rating': 0.93
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=gaVNVrqSJN8dVzubVaqXamZcZOhVeDxOCdIybLbf6C8=; b=c2bQQzu7j7xD4tmOMRSaKwr8DOtdjOFiPE2QFtmfUx9v+SyTcbrayB2O0ut47Zj0vX yIrm/tJmJkztkgxelsegSMyqHuQ6kR7vVck6iiyIEEygGtWXOTW3bXOYZpZRWUu63qZ1 DUq0NlMtPeKS8KzwCST2CN+TpbjLJoijcok8gbnTENLfxSfPzXVyikRZQMoEmC5FdrZB W9D6bCSIq9Q3tjZeHf46u7vTN0JWH9Et+deL4l3Pa7KEnz5H7aAaS+IuY+i0vBPx725i Uswot+lBAjpbegDa8uq8FaLZu4lbtClI9YYmTkypD6Nr10RDs5LmNmJ7xKk9k/LiE0AE Opwg==
MIME-Version 1.0
Date Wed, 17 Oct 2012 16:47:09 +1100
Subject ElementTree Issue - Search and remove elements
From Tharanga Abeyseela <tharanga.abeyseela@gmail.com>
To python-list@python.org
Content-Type text/plain; charset=ISO-8859-1
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.2323.1350452831.27098.python-list@python.org> (permalink)
Lines 78
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1350452831 news.xs4all.nl 6890 [2001:888:2000:d::a6]:38353
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:31469

Show key headers only | View raw


Hi Guys,

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')


Really appreciate your thoughts on this.

Thanks in advance,
Tharanga

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


Thread

ElementTree Issue - Search and remove elements Tharanga Abeyseela <tharanga.abeyseela@gmail.com> - 2012-10-17 16:47 +1100
  Re: ElementTree Issue - Search and remove elements Alain Ketterlin <alain@dpt-info.u-strasbg.fr> - 2012-10-17 08:25 +0200
    Re: ElementTree Issue - Search and remove elements Stefan Behnel <stefan_ml@behnel.de> - 2012-10-17 09:01 +0200

csiph-web