Get list of XML attribute values in Python

前端 未结 7 1563
有刺的猬
有刺的猬 2020-12-31 07:37

I need to get a list of attribute values from child elements in Python.

It\'s easiest to explain with an example.

Given some XML like this:

&         


        
7条回答
  •  南方客
    南方客 (楼主)
    2020-12-31 07:46

    I must admit I'm a fan of xmltramp due to its ease of use.

    Accessing the above becomes:

      import xmltramp
    
      values = xmltramp.parse('''...''')
    
      def getValues( values, category ):
        cat = [ parent for parent in values['parent':] if parent(name) == category ]
        cat_values = [ child(value) for child in parent['child':] for parent in cat ]
        return cat_values
    
      getValues( values, "CategoryA" )
      getValues( values, "CategoryB" )
    

提交回复
热议问题