开发者

LXML itertree parsing tag information

I have been searching around for answers, but I can'开发者_如何学编程t seem to find anything. Using lxml to iterparse through xml, I have been trying to get data from within a tag.

For example, I have the xml:

<line number="0">Line 1</line>
<line number="1">Line 2</line> 
<line number="2">Line 3</line> 

I need to be able to read < line number="2" >. Printing out the tag only includes < line >.

Thanks!

Edit: rczajka answered my question (thank you!), but I was wondering: How do you do this abstractly -- without saying 'number'? The xml I'm parsing is gigantic, and I can't exactly scroll through all of it to see if there is anything hidden in the tags.

rczaika: elem.items() will give you a list of all attributes.

Thanks!


To get information from an etree element you can do:

>>> elem.tag
'line'
>>> elem.get('number')
'2'
>>> elem.text
'Line 3'`
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜