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


Groups > comp.lang.python > #110178

Re: lxml - SubElement dump root doesn't dump like Element dump does

Path csiph.com!fu-berlin.de!uni-berlin.de!not-for-mail
From Peter Otten <__peter__@web.de>
Newsgroups comp.lang.python
Subject Re: lxml - SubElement dump root doesn't dump like Element dump does
Date Mon, 20 Jun 2016 08:19 +0200
Organization None
Lines 110
Message-ID <mailman.149.1466403557.2288.python-list@python.org> (permalink)
References <8a1bbff2-14a4-4a1a-b53f-ca94f94d54bc@googlegroups.com> <nk81sq$khk$1@ger.gmane.org>
Mime-Version 1.0
Content-Type text/plain; charset="ISO-8859-1"
Content-Transfer-Encoding 7Bit
X-Trace news.uni-berlin.de u6zzsq/u+/bktvnhQNOTRA6lmHDMMyP/VwDKU0qMsF5g==
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; 'skip:[ 20': 0.03; 'root': 0.04; '"""': 0.05; 'filename': 0.07; 'expected.': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'example:': 0.10; 'def': 0.13; "'0'": 0.16; "'http://'": 0.16; 'appends': 0.16; 'attributes.': 0.16; 'element.': 0.16; 'f.read()': 0.16; 'open(file)': 0.16; 'received:80.91.229.3': 0.16; 'received:dip0.t-ipconnect.de': 0.16; 'received:io': 0.16; 'received:plane.gmane.org': 0.16; 'received:psf.io': 0.16; 'received:t-ipconnect.de': 0.16; 'wrote:': 0.16; 'element': 0.18; 'instance,': 0.18; 'skip:l 30': 0.18; 'creates': 0.18; '>>>': 0.20; 'library': 0.20; 'referring': 0.22; 'elements': 0.23; 'cheers': 0.24; 'xml': 0.24; 'wondering': 0.25; 'module': 0.25; 'header:User-Agent:1': 0.26; 'header:X-Complaints-To:1': 0.26; 'handling': 0.27; 'yield': 0.27; 'specifically': 0.28; 'function': 0.28; 'meeting': 0.30; 'code': 0.30; 'venue': 0.30; 'skip:[ 10': 0.31; "i'd": 0.31; 'anyone': 0.32; 'point': 0.33; 'club': 0.33; 'subject:like': 0.33; "skip:' 20": 0.34; 'file': 0.34; 'gives': 0.35; 'quite': 0.35; 'expected': 0.35; 'skip:p 30': 0.35; 'but': 0.36; 'to:addr:python-list': 0.36; 'subject:: ': 0.37; 'say': 0.37; 'received:org': 0.37; 'skip:p 20': 0.38; 'skip:o 20': 0.38; 'why': 0.39; 'to:addr:python.org': 0.40; 'received:de': 0.40; 'accessed': 0.66; 'repeat': 0.67; 'race': 0.72; "'one'": 0.84; 'weather': 0.84
X-Injected-Via-Gmane http://gmane.org/
X-Gmane-NNTP-Posting-Host p57bd9d43.dip0.t-ipconnect.de
User-Agent KNode/4.13.3
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.22
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>
X-Mailman-Original-Message-ID <nk81sq$khk$1@ger.gmane.org>
X-Mailman-Original-References <8a1bbff2-14a4-4a1a-b53f-ca94f94d54bc@googlegroups.com>
Xref csiph.com comp.lang.python:110178

Show key headers only | View raw


Sayth Renshaw wrote:

> Afternoon
> 
> Wondering has anyone much experience with lxml specifically objectify?
> 
> When I pick up a file with lxml and use objectify dumping root works as
> expected actually better its quite nice. This is how i do it, file
> handling part left out for brevity.
> 
> def getsMeet(file_list):
>     for filename in sorted(file_list):
>         filename=my_dir + filename
>         yield filename
> 
> def parseXML():
>     """
>     """
>     for file in getsMeet(file_list):
>         with open(file) as f:
>             xml = f.read()
> 
>             root = objectify.fromstring(xml)
>             print(root.tag)
>             print(objectify.dump(root))
>             race = objectify.SubElement(root,"race")
>             print(objectify.dump(race))
> 
> 
> parseXML()
> 
> So the first call to print(objectify.dump(root)) gives as a sample.
> 
> meeting = None [ObjectifiedElement]
>   * id = '42977'
>   * barriertrial = '0'
>   * venue = 'Rosehill Gardens'
>   * date = '2016-05-21T00:00:00'
>   * gearchanges = '-1'
>   * stewardsreport = '-1'
>   * gearlist = '-1'
>   * racebook = '0'
>   * postracestewards = '0'
>   * meetingtype = 'TAB'
>   * rail = 'Timing - Electronic : Rail - +6m'
>   * weather = 'Fine      '
>   * trackcondition = 'Good 3    '
>   * nomsdeadline = '2016-05-16T11:00:00'
>   * weightsdeadline = '2016-05-17T16:00:00'
>   * acceptdeadline = '2016-05-18T09:00:00'
>   * jockeydeadline = '2016-05-18T12:00:00'
>     club = '' [StringElement]
>       * abbrevname = 'Australian Turf Club'
>       * code = '56398'
>       * associationclass = '1'
>       * website = 'http://'
>     race = None [ObjectifiedElement]
>       * id = '215411'
>       * number = '1'
>       * nomnumber = '9'
> 
> Then I am confused when I want to repeat this but only for the subelement
> race I get a return but not as expected.
> 
> This is my return
> race = '' [StringElement]
> 
> so why do i not get all the elements of race as I do when i dump the root?

Because race is a new SubElement that you just created:

>>> help(lxml.objectify.SubElement)
Help on built-in function SubElement in module lxml.etree:

SubElement(...)
    SubElement(_parent, _tag, attrib=None, nsmap=None, **_extra)
    
    Subelement factory.  This function creates an element instance, and
    appends it to an existing element.

>>>

Existing subelements can be accessed as attributes. I'd say that's the very 
point of the lxml.objectify library ;)

For example:

>>> root = 
lxml.objectify.fromstring("<a><b><c>one</c><d>two</d></b><b>second 
b</b></a>")
>>> print(lxml.objectify.dump(root.b))
b = None [ObjectifiedElement]
    c = 'one' [StringElement]
    d = 'two' [StringElement]
>>> print(lxml.objectify.dump(root.b[0]))
b = None [ObjectifiedElement]
    c = 'one' [StringElement]
    d = 'two' [StringElement]
>>> print(lxml.objectify.dump(root.b[1]))
b = 'second b' [StringElement]

> Cheers
> 
> Sayth
> PS I am referring to this documentation
> http://lxml.de/objectify.html#element-access-through-object-attributes

Read that again.

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


Thread

lxml - SubElement dump root doesn't dump like Element dump does Sayth Renshaw <flebber.crue@gmail.com> - 2016-06-19 20:51 -0700
  Re: lxml - SubElement dump root doesn't dump like Element dump does Peter Otten <__peter__@web.de> - 2016-06-20 08:19 +0200
    Re: lxml - SubElement dump root doesn't dump like Element dump does Sayth Renshaw <flebber.crue@gmail.com> - 2016-06-20 00:13 -0700
    Re: lxml - SubElement dump root doesn't dump like Element dump does Sayth Renshaw <flebber.crue@gmail.com> - 2016-06-20 01:21 -0700

csiph-web