Elementtree乱序显示元素 [英] Elementtree displaying elements out of order
问题描述
我正在使用Python的ElementTree解析xml文件.我有一个"findall"来查找所有"revision"子元素,但是当我遍历结果时,它们不是按文档顺序排列的.我做错了什么?
这是我的代码:
allrevisions = page.findall('{http://www.mediawiki.org/xml/export-0.5/} revision')对于所有版本的修订:打印版本打印rev.find('{http://www.mediawiki.org/xml/export-0.5/} timestamp').text
以下是我正在解析的文档的链接: http://pastie.org/2780983 >
谢谢,bsg
-糟糕.通过遍历我的代码并逐个运行它,我解决了问题-我在元素列表中的反向()位置卡在了错误的位置,这引起了所有麻烦.非常感谢您的帮助-很抱歉这是一个愚蠢的问题.
结果: 查看正在解析的文档会很有帮助. 更新: 使用您提供的源数据:1个2个34
导入with_statement将xml.etree.ElementTree导入为使用open('xmldata.xml','r')作为f:xmldata = f.read()树= et.fromstring(xmldata)在tree.findall中进行修订('.//{http://www.mediawiki.org/xml/export-0.5/}修订'):打印修订版.find('{http://www.mediawiki.org/xml/export-0.5/} text').text [0:10] .encode('utf8')
结果:
‘思想{{db-spam}‘思想敏< ;!-恳求
与文档中显示的顺序相同.
I'm using Python's ElementTree to parse xml files. I have a "findall" to find all "revision" subelements, but when I iterate through the result, they are not in document order. What can I be doing wrong?
Here's my code:
allrevisions = page.findall('{http://www.mediawiki.org/xml/export-0.5/}revision')
for rev in allrevisions:
print rev
print rev.find('{http://www.mediawiki.org/xml/export-0.5/}timestamp').text
Here's a link to the document I'm parsing: http://pastie.org/2780983
Thanks, bsg
-Oops. By going through my code and running it piece by piece, I worked out the problem - I had stuck in a reverse() on the elements list in the wrong place, which was causing all the trouble. Thank you so much for your help - I'm sorry it was such a silly issue.
The documentation for ElementTree says that findall
returns the elements in document order.
A quick test shows the correct behaviour:
import xml.etree.ElementTree as et
xmltext = """
<root>
<number>1</number>
<number>2</number>
<number>3</number>
<number>4</number>
</root>
"""
tree = et.fromstring(xmltext)
for number in tree.findall('number'):
print number.text
Result:
1 2 3 4
It would be helpful to see the document you are parsing.
Update:
Using the source data you provided:
from __future__ import with_statement
import xml.etree.ElementTree as et
with open('xmldata.xml', 'r') as f:
xmldata = f.read()
tree = et.fromstring(xmldata)
for revision in tree.findall('.//{http://www.mediawiki.org/xml/export-0.5/}revision'):
print revision.find('{http://www.mediawiki.org/xml/export-0.5/}text').text[0:10].encode('utf8')
Result:
‘The Mind {{db-spam} ‘The Mind '''The Min <!-- Pleas
The same order as they appear in the document.
这篇关于Elementtree乱序显示元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!