LXML cannot access elements of dict created

Sayth Renshaw flebber.crue at gmail.com
Fri Jun 24 00:18:26 EDT 2016


I have created several varitions of my parser however I am still having the same issue, ,my return is the whole file, not a key of the dict as i have attempted to access.

Not really sure where I am going wrong with it.

I have left previous implementations in however to show where I have come from, as a first test case i am trying to parse the file and then return the dict key numbers and all its values.

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