python的lxml和iterparse方法

发布于 2024-10-28 03:51:36 字数 1044 浏览 3 评论 0原文

假设我有这个示例 XML。

<result>
    <field k='field1'>
        <value h='1'><text>text_value1</text></value>
    </field>
    <field k='field2'>
        <value><text>text_value2</text></value>
    </field>
    <field k='field3'>
        <value><text>some_text</text></value>
    </field>
</result>

使用python的lxml,如何获取每个结果集的每个字段的值?所以基本上,我想迭代所有结果集,然后迭代该结果集中的每个字段并打印文本数据。

这就是我到目前为止所拥有的:

context = etree.iterparse(contentBuffer, tag='result')
for action, elem in context:
    print elem.tag, elem.data

任何帮助将不胜感激。

编辑 这是我想出的代码。必须调用 getparent() 两次来读取相应文本值的属性,这似乎有点笨拙。有更好的方法吗?

for action, elem in context:
    list = elem.xpath('//text')
    print "result set:"
    for item in list:
        field = item.getparent().getparent().attrib['k']
        value = item.text
        print "\t%s = %s"%(field, value)

Say i have this sample XML.

<result>
    <field k='field1'>
        <value h='1'><text>text_value1</text></value>
    </field>
    <field k='field2'>
        <value><text>text_value2</text></value>
    </field>
    <field k='field3'>
        <value><text>some_text</text></value>
    </field>
</result>

Using python's lxml, how can i get the value of each field for every result set? So basically, i want to iterate over ever result set, then iterate over every field in that result set and print the text data.

This is what i have so far:

context = etree.iterparse(contentBuffer, tag='result')
for action, elem in context:
    print elem.tag, elem.data

Any help would be greatly appreciated.

EDIT
Here is the code that i came up with. It seems a bit clunky having to call getparent() twice to read the attribute of corresponding text value. Is there a better way to do this?

for action, elem in context:
    list = elem.xpath('//text')
    print "result set:"
    for item in list:
        field = item.getparent().getparent().attrib['k']
        value = item.text
        print "\t%s = %s"%(field, value)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

爱,才寂寞 2024-11-04 03:51:36

怎么样:

import io
import lxml.etree as ET

content='''\
<result>
    <field k='field1'>
        <value h='1'><text>text_value1</text></value>
    </field>
    <field k='field2'>
        <value><text>text_value2</text></value>
    </field>
    <field k='field3'>
        <value><text>some_text</text></value>
    </field>
</result>'''

contentBuffer=io.BytesIO(content)
context = ET.iterparse(contentBuffer,tag='result')
for action, elem in context:
    fields=elem.xpath('field/@k')
    values=elem.xpath('field/value/text/text()')
    for field,value in zip(fields,values):
        print('\t{f} = {v}'.format(f=field,v=value))

哪个产量

field1 = text_value1
field2 = text_value2
field3 = some_text

How about:

import io
import lxml.etree as ET

content='''\
<result>
    <field k='field1'>
        <value h='1'><text>text_value1</text></value>
    </field>
    <field k='field2'>
        <value><text>text_value2</text></value>
    </field>
    <field k='field3'>
        <value><text>some_text</text></value>
    </field>
</result>'''

contentBuffer=io.BytesIO(content)
context = ET.iterparse(contentBuffer,tag='result')
for action, elem in context:
    fields=elem.xpath('field/@k')
    values=elem.xpath('field/value/text/text()')
    for field,value in zip(fields,values):
        print('\t{f} = {v}'.format(f=field,v=value))

which yields

field1 = text_value1
field2 = text_value2
field3 = some_text
一曲爱恨情仇 2024-11-04 03:51:36

我建议使用 XPath 查询r = tree.xpath('//text') 中的内容应该足以满足您的需求。

I would suggest to use a XPath query. Something along r = tree.xpath('//text') should be sufficient for your needs.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文