Home > Net >  AttributeError after sorting through elementtree values
AttributeError after sorting through elementtree values

Time:02-08

I'm trying to take elementtree elements picked from an XML and sort them through alphabetical order. Since there's multiple XMLs I'm sorting through (keys are defined in the platforms list), I'm first storing the values in a dictionary with the key and then sorting them.

I've tried the following:

titles = {}

for platform in platforms:
    for title in self.databases[platform][1].findall("game"):
        if title.find("locale", {"lang": "EN"}) is not None:
            titles[title.find("locale", {"lang": "EN"}).find("title").text   " - "   platform] = title
            print(title.find("region").text) # this works

for key, title in sorted(titles.items()):
    for s in title:
        t = s.find("region").text

However, I'm getting AttributeError: 'NoneType' object has no attribute 'text' with the last line of the code, which doesn't make sense because I'm able to print the XML value in the loop where I assign the values to a dictionary.

2 questions:

  1. Is there a better way to do this? I'm sure that this can be optimized in a better way. Maybe elementtree or Python has a nice little function that can sort when the items are stored as elements?
  2. Why am I getting an AttributeError? Is something breaking after I store them in a dictionary?

CodePudding user response:

With regard to question 2. Why the line "for s in title" (is title an iterator)? While title.find(region) has a text attribute it is not clear that s.find("region") does

  •  Tags:  
  • Related