通过 elementtree 值排序后的 AttributeError

AttributeError after sorting through elementtree values

我正在尝试从 XML 中选取 elementtree 元素并按字母顺序对它们进行排序。由于我要对多个 XML 进行排序(键在 platforms 列表中定义),我首先将值与键一起存储在字典中,然后对它们进行排序。

我试过以下方法:

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

但是,我在代码的最后一行得到 AttributeError: 'NoneType' object has no attribute 'text',这没有意义,因为我能够在分配的循环中打印 XML 值字典的值。

2 个问题:

  1. 有更好的方法吗?我确信这可以以更好的方式进行优化。也许 elementtree 或 Python 有一个不错的小功能,可以在项目存储为元素时进行排序?
  2. 为什么我得到 AttributeError?我将它们存储在字典中后,有什么东西坏了吗?

关于问题2,为什么“for s in title”这一行(title是一个迭代器)?虽然 title.find(region) 具有文本属性,但尚不清楚 s.find("region")