Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #110168 > unrolled thread
| Started by | Sayth Renshaw <flebber.crue@gmail.com> |
|---|---|
| First post | 2016-06-19 20:51 -0700 |
| Last post | 2016-06-20 01:21 -0700 |
| Articles | 4 — 2 participants |
Back to article view | Back to comp.lang.python
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
| From | Sayth Renshaw <flebber.crue@gmail.com> |
|---|---|
| Date | 2016-06-19 20:51 -0700 |
| Subject | lxml - SubElement dump root doesn't dump like Element dump does |
| Message-ID | <8a1bbff2-14a4-4a1a-b53f-ca94f94d54bc@googlegroups.com> |
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?
Cheers
Sayth
PS I am referring to this documentation http://lxml.de/objectify.html#element-access-through-object-attributes
[toc] | [next] | [standalone]
| From | Peter Otten <__peter__@web.de> |
|---|---|
| Date | 2016-06-20 08:19 +0200 |
| Message-ID | <mailman.149.1466403557.2288.python-list@python.org> |
| In reply to | #110168 |
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.
[toc] | [prev] | [next] | [standalone]
| From | Sayth Renshaw <flebber.crue@gmail.com> |
|---|---|
| Date | 2016-06-20 00:13 -0700 |
| Message-ID | <b76ee148-cba1-44a1-8727-bc23ef9e0560@googlegroups.com> |
| In reply to | #110178 |
Thanks your way makes more sense indeed. In the example they create and access I think I just got lost in their example. Sayth
[toc] | [prev] | [next] | [standalone]
| From | Sayth Renshaw <flebber.crue@gmail.com> |
|---|---|
| Date | 2016-06-20 01:21 -0700 |
| Message-ID | <9c0595de-4e61-4f32-9a17-f1e2d53a12b0@googlegroups.com> |
| In reply to | #110178 |
On Monday, 20 June 2016 16:19:31 UTC+10, Peter Otten wrote:
> 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]
>
this actually seems quite powerful, I don't fully understand it yet though it seems I have just got an XML doc as a list of dicts so I can just slice and select as normal python.
thanks for helping
Sayth
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.python
csiph-web