简体   繁体   English

遍历XML元素列表的属性的Pythonic方法?

[英]Pythonic way to iterate over attributes of a list of XML elements?

I have xml data that looks like this: 我有看起来像这样的xml数据:

<person name="kyle" favoritefood="ham" favoritesport="baseball" />
<person name="sarah" favoritefood="chicken" favoritesport="basketball" />
<person name="susan" favoritefood="tuna" favoritesport="soccer" />

There's no nesting, I'm only interested in those attributes name , favoritefood , and favoritesport . 没有嵌套,我只对那些属性namefavoritefoodfavoritesport感兴趣。

I'm wondering what the most pythonic way to do this is. 我想知道最pythonic的方法是什么。

One idea I had was 我的一个想法是

people = ET.parse('file.txt').getroot().findall('person')
for person in people:
    name = person.get('name')
    favoritefood = person.get('favoritefood')
    favoritesport = person.get('favoritesport')
    ...

But that requires a lot of unnecessary lines of code just for getting the attributes. 但这仅需要获取属性就需要很多不必要的代码行。 It seems redundant since I'm typing all the attributes out twice. 因为我要两次键入所有属性,所以这似乎是多余的。

Another idea was 另一个想法是

people = ET.parse('file.txt').getroot().findall('person')
for name, favoritefood, favoritesport in [(person.get('name'), person.get('favoritefood'), person.get('favoritesport')) for person in people]:
    ...

But as you can see that line's really long, and will get even longer if I wanted to tag an if filter to the end of that list comprehension. 但是正如您所看到的那样,该行的确很长,而且如果我想将if过滤器标记到该列表理解的末尾,该行将变得更长。

Is there a more pythonic way to do this? 有没有更Python的方式来做到这一点?

I did not challenge naming convention as then my sample could be misleading. 我没有挑战命名约定,因为这样我的示例可能会产生误导。 But this is my vote 但这是我的投票

But my focus would be using a context manager to load the file and then apply then methods then 但我的重点是使用上下文管理器加载文件,然后应用然后使用方法

with ET.parse('file.txt').getroot().findall('person') as people:
    [...]

So now, we go to the inner method. 所以现在,我们转到内部方法。

The first version is longer, but more readable to new programmers. 第一个版本较长,但对新程序员更易读。 The second one uses list comprehension ans therefore shorter. 第二个使用列表理解,因此更短。 I would go for the second option using list comprehension. 我将使用列表理解来进行第二种选择。 I would make sure that the line length does not exceed 100 chars and break it. 我将确保行长不超过100个字符并中断它。

You can create a tuple of attributes you are interested in, and then have a dict comprehension inside a list comprehension. 您可以创建一个您感兴趣的属性的元组,然后在列表推导中进行字典推导。 This way you don't repeat yourself and the code is short and concise (albeit may be harder for a novice to grasp). 这样,您就不必重复自己了,代码简短明了(尽管对于新手来说可能更难掌握)。

required_attributes = ('name', 'favoritefood', 'favoritesport')
list_of_people = [{attribute: person.get(attribute) for attribute in required_attributes} 
                  for person in persons]

print(list_of_people)
# [{'name': 'kyle', 'favoritefood': 'ham', 'favoritesport': 'baseball'},
#  {'name': 'sarah', 'favoritefood': 'chicken', 'favoritesport': 'basketball'}, 
#  {'name': 'susan', 'favoritefood': 'tuna', 'favoritesport': 'soccer'}]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM