I seem to be creating a dict that I cannot access the keys of
Sayth Renshaw
flebber.crue at gmail.com
Thu Jun 23 23:59:59 EDT 2016
Really getting a little lost here with lxml.
I cannot seem to create a dict of results using xpath or other that I could easily get the results from, currently I am always returned a printed copy of the whole file regardless of what parsing options I do.
Can I ask is there something obvious wrong, i have left the commented other versions in they "work" in that they all produce the same output the print whole file.
def parseXML():
"""
given a file XML will parse for listed attributes.
using objectified lxml
"""
for file in getsMeet(file_list):
with open(file, "rb") as f:
xml = f.read()
root = objectify.parse(xml)
atts = ("number", "id", "horse", "saddlecloth", "barrier",
"weight", "rating", "description", "colours", "owners",
"dob", "age", "sex", "career", "thistrack", "thisdistance",
"goodtrack", "heavytrack", "finished", "weightvariation",
"variedweight", "decimalmargin", "penalty",
"pricestarting")
tree = etree.parse(StringIO(xml))
result = etree.tostring(tree.getroot(), pretty_print=True)
for sample in result:
noms = (dict(zip(atts, map(sample.attrib.get, atts))))
print(noms)
# print(etree.tostring(tree.getroot()))
#
# for sample in root.xpath('//race/nomination'):
# noms = (dict(zip(atts, map(sample.attrib.get, atts))))
# numbers = [(k, v) for k, v in noms.items()]
# print(noms)
# return numbers
# parser = etree.XMLParser(root, remove_comments=True)
# d = defaultdict(list)
# for sample in parser.xpath('//race/nomination'):
# print(sample)
# for sample in root.xpath('//race/nomination'):
# # print(sample)
# dct = sample.attrib
# for k in atts:
# # print(k)
# d[k].append(dct[k])
# print(d["number"])
return noms
a = parseXML()
for sample in a.items:
print(sample["number"])
Confused
Sayth
More information about the Python-list
mailing list