I seem to be creating a dict that I cannot access the keys of
Sayth Renshaw
flebber.crue at gmail.com
Sat Jun 25 04:41:28 EDT 2016
>
> The code below is obviously wrong - it is surprising that you get
> anything other than an exception. See comments below inserted into
> your code.
>
> > def parseXML():
> > ...
> > result = etree.tostring(tree.getroot(), pretty_print=True)
>
> "result" here is obviously a string.
>
> > for sample in result:
>
> This means that "sample" successively hold the characters composing
> the string "result".
>
> > noms = (dict(zip(atts, map(sample.attrib.get, atts))))
>
> You should get "AttributeError: str object does not have attribute `attrib`".
The attrib is an lxml function, when you have defined a root
http://lxml.de/tutorial.html
>>> attributes = root.attrib
or
>>> d = dict(root.attrib)
>>> sorted(d.items())
[('hello', 'Guten Tag'), ('interesting', 'totally')]
if I run it with this section as you reference above
for sample in root.xpath('//race/nomination'):
noms = (dict(zip(atts, map(sample.attrib.get, atts))))
print(noms)
I get working output
(pyxml) [sayth at localhost pyXML]$ python xrace.py data/ -e .xml
{'barrier': '5', 'horse': 'Chipanda', 'goodtrack': '0-0-0-0', 'weight': '54', 'pricestarting': '$3.50', 'age': '3', 'description': 'B F 2 Sepoy x Lobola (Anabaa(USA))', 'heavytrack': '0-0-0-0', 'career': '2-0-0-2 $30225.00', 'colours': 'Royal Blue', 'finished': '1', 'owners': 'Godolphin ', 'dob': '2013-10-08T00:00:00', 'id': '198926', 'thisdistance': '0-0-0-0', 'weightvariation': '0', 'penalty': '0', 'number': '8', 'rating': '0', 'sex': 'F', 'decimalmargin': '0.00', 'variedweight': '54', 'saddlecloth': '8', 'thistrack': '1-0-0-1 $15000.00'}
{'barrier': '1', 'horse': 'Legerity', 'goodtrack': '3-1-0-1 $77150.00', 'weight': '57.5', 'pricestarting': '$2.50F', 'age': '3', 'description': 'B C 2 Snitzel x
Cheers
Sayth
More information about the Python-list
mailing list