lxml parsing whole file, unable to access elements

Sayth Renshaw flebber.crue at gmail.com
Fri Jun 24 00:07:39 EDT 2016


I have created several versions of a parser for XML with lxml both objectify and lxml. They all work in that the parser returns the whole file however I cannot access elements or keys as I have tried to create a dict of the results to make it easier to put in an sql query later.

However I keep receiving output as just the whole file, I have gone backwards since last time, learnt more however output is backwards.

Is there something obvious here I am doing wrong, all I am intending to do as a first test is to get the result and then get the key "number" and return that key and all its values.

i have left the commented versions in as well they all end in the same result to me.

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",

            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(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:



More information about the Python-list mailing list