My Generator yields a dictionary. Return entire file not specified key, value
Sayth Renshaw
flebber.crue at gmail.com
Thu Jun 23 21:34:16 EDT 2016
When I am running my file which takes a file off the command line reads it and creates an objecitifed xml instance and then parses for each attribute into a dictionary and then yields it.
At this point I am tring to use it to obtain specific key value elements, however I keep being returned the whole file output rather than the selection key.really not sure where I am going wrong.
The key part of my file that is using the generator is
for item in parseXML():
number = item["number"]
print(number)
Rest of file for reference
================================================================================
from lxml import objectify
import argparse
import os
parser = argparse.ArgumentParser()
parser.add_argument("path", type=str, nargs="+")
parser.add_argument('-e',
'--extension',
default='',
help='File extension to filter by.')
args = parser.parse_args()
name_pattern = "*" + args.extension
my_dir = args.path[0]
for dir_path, subdir_list, file_list in os.walk(my_dir):
for name_pattern in file_list:
full_path = os.path.join(dir_path, name_pattern)
def getsMeet(file_list):
"""generator which yields sorted file."""
for filename in sorted(file_list):
filename = my_dir + filename
yield filename
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")
for sample in root.xpath('//race/nomination'):
noms = (dict(zip(atts, map(sample.attrib.get, atts))))
yield noms
for item in parseXML():
number = item["number"]
print(number)
================================================================================
Thanks
Sayth
More information about the Python-list
mailing list