开发者

How to read and parse XML without schema in Python?

Is there a way to read an XML document in Python without the schema? In my us开发者_高级运维e case there is a file similar to the following.

<people>
    <human>
      <weight>75</weight>
      <height>174</height>
    </human>
    <human>
      <weight>89</weight>
      <height>187</height>
    </human>
</people>

I need to extract an array of weight from it. It can easily be done with string manipulation but there must be a cleaner way to do that with XML parser?


You could use ElementTree (included in the python standard library) and do the following:

import xml.etree.ElementTree
tree = xml.etree.ElementTree.parse("foo.xml")
myArray = [int(x.text) for x in tree.getroot().findall("human/weight")]
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜