python - 为 XML 文件 Python 中的子元素添加索引
问题描述
我是python的新手,所以请帮忙。我想以与元素具有索引相同的方式向row
和元素添加索引。column
page
在第 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>
我希望有意义。提前致谢。
解决方案
见下文
('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>
推荐阅读
- php - 如何将 XML 文件中的值添加到 php 中的 csv 文件?
- java - 肥皂请求 XML。来自 WSDL
- graphql - Hasura 选择子项包含当前用户的项目
- javascript - 如何在 typescript 中使用 Cypress 监视导入的函数
- visual-studio-code - 是否可以从 clangd vscode 扩展中获取 cpp 文件的 AST?
- go - .locfatal:不是 git 存储库(或任何父目录):.git 在 bash shell 中自动完成
- javascript - getElementsByClassName.innerhtml/getElementsByTagName.innerhtml 不替换内容
- arrays - 如何有效地比较二维数组
- javascript - 范围数组,包括最小值、最大值和范围数
- spring - 在java中的get请求中发送Json