向XML文件Python中的子元素添加索引

时间:2019-06-01 04:33:23

标签: python xml indexing

我是python的新手,所以请帮忙。我想以row元素具有索引的相同方式向columnpage元素添加索引。

在第1页中有4行,因此索引从0到3。在第1页中,第0行只有一列,因此索引只有0。 在第1页的第2行中,有3列,因此列的索引将从0变为2。其他页面中的其他行也是如此。

我已经开始使用Elementree进行测试,但仅测试打印元素的基础知识。也许有人可以帮我这个忙。

我仅具有以下代码用于基本测试,但我不知道如何进行此操作。

import xml.etree.ElementTree as ET
tree = ET.parse('smp.xml')
root = tree.getroot()

for text in root.iter('text'):
    print(text.attrib)

for text in root.iter('text'):
    print(text.text)

输入XML如下:

<?xml version="1.0"?>
<doc>
    <page index="0"/>
    <page index="1">
        <row>
            <column>
                <text>fibrous drupe</text>
            </column>
        </row>
        <row>
            <column>
                <text>follicle</text>
            </column>
            <column>
                <text>legume</text>
            </column>
        </row>
        <row>
            <column>
                <text>loment</text>
            </column>
            <column>
                <text>nut</text>
            </column>
            <column>
                <text>samara</text>
            </column>
        </row>
        <row>
            <column>
                <text>schizocarp</text>
            </column>
        </row>
    </page>
    <page index="2">
        <row>
            <column>
                <text>cypsela</text>
            </column>
        </row>
    </page>
    <page index="3"/>
</doc>

,我想将其转换为此:

<?xml version="1.0"?>
<doc>
    <page index="0"/>
    <page index="1">
        <row index="0">
            <column index="0">
                <text>fibrous drupe</text>
            </column>
        </row>
        <row index="1">
            <column index="0">
                <text>follicle</text>
            </column>
            <column index="1">
                <text>legume</text>
            </column>
        </row>
        <row index="2">
            <column index="0">
                <text>loment</text>
            </column>
            <column index="1">
                <text>nut</text>
            </column>
            <column index="2">
                <text>samara</text>
            </column>
        </row>
        <row index="3">
            <column index="0">
                <text>schizocarp</text>
            </column>
        </row>
    </page>
    <page index="2">
        <row index="0">
            <column index="0">
                <text>cypsela</text>
            </column>
        </row>
    </page>
    <page index="3"/>
</doc>

我希望有道理。预先感谢。

2 个答案:

答案 0 :(得分:1)

我是python的新手,所以您必须自己完成此操作:

import xml.etree.ElementTree as ET
tree = ET.parse('smp.xml')
root = tree.getroot()

for text in root:
    print(text.tag, text.attrib)
    for text2 in text:
        print(" ", text2.tag, text2.attrib)
        if (text2.tag=='row'):
           text2.set('index','42')

tree.write('output.xml')

在“ output.xml”中,您将获得:

<doc>
    <page index="0" />
    <page index="1">
        <row index="42">
            <column>
                <text>fibrous drupe</text>
            </column>
        </row>
        <row index="42">
            <column>
                <text>follicle</text>
            </column>
            <column>
                <text>legume</text>
            </column>
        </row>
        <row index="42">
            <column>
               ….

因此,您需要将那些“ 42”更改为所需的值。

答案 1 :(得分:1)

请参见下文

(“ 56403870.xml”是您发布的XML)

import xml.etree.ElementTree as ET

tree = ET.parse('56403870.xml')
root = tree.getroot()

pages = root.findall('.//page')
for page in pages:
    rows = page.findall('.//row')
    for r, row in enumerate(rows):
        row.attrib['index'] = str(r)
        columns = row.findall('.//column')
        for c, col in enumerate(columns):
            col.attrib['index'] = str(c)

ET.dump(tree)

输出

<doc>
    <page index="0" />
    <page index="1">
        <row index="0">
            <column index="0">
                <text>fibrous drupe</text>
            </column>
        </row>
        <row index="1">
            <column index="0">
                <text>follicle</text>
            </column>
            <column index="1">
                <text>legume</text>
            </column>
        </row>
        <row index="2">
            <column index="0">
                <text>loment</text>
            </column>
            <column index="1">
                <text>nut</text>
            </column>
            <column index="2">
                <text>samara</text>
            </column>
        </row>
        <row index="3">
            <column index="0">
                <text>schizocarp</text>
            </column>
        </row>
    </page>
    <page index="2">
        <row index="0">
            <column index="0">
                <text>cypsela</text>
            </column>
        </row>
    </page>
    <page index="3" />
</doc>