简体   繁体   English

在lxml objectify中获取有序列表

[英]Get an ordered list in lxml objectify

If I have an ordered list in XML 如果我有一个XML的有序列表

<Boa>
    <Channels>
        <Channel name="Red"/>
        <Channel name="Green"/>
        <Channel name="Blue" />
    </Channels>
</Boa>

This code 此代码

from lxml import objectify

Boa = objectify.parse(self.xml).getroot()

only gets me 只让我

Boa.Channels.Channel

with a single entry for Channel. 并为Channel输入一个条目

How do I get this as an ordered list in lxml objectify? 如何在lxml objectify中将其作为有序列表获取? I'm also fine with changing my XML markup if there's something that lxml expects to automatically do the conversion. 如果lxml希望自动进行转换,我也可以更改XML标记。

objectify is a bit weird as it tries to map xml to python objects and this is not 100% match so it has to compromise. objectify有点奇怪,因为它尝试将xml映射到python对象,并且不是100%匹配,因此必须妥协。

Boa.Channels.Channel is the first Channel Boa.Channels.Channel是第一个Channel

>>> Boa.Channels.Channel.get('name')
'Red'

But at the same time it can also work as a list of Channel s: 但同时,它也可以作为Channel的列表:

>>> Boa.Channels.Channel[0].get('name')
'Red'
>>> Boa.Channels.Channel[1].get('name')
'Green'
>>> Boa.Channels.Channel[2].get('name')
'Blue'
>>> [c.get('name') for c in Boa.Channels.Channel]
['Red', 'Green', 'Blue']

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

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