根据特殊标志从XML文件中删除行

2024-10-01 19:28:31 发布

您现在位置:Python中文网/ 问答频道 /正文

我的文件包含不同的标记。你知道吗

根据标记,我想根据标记中的值删除行。你知道吗

例如,cellColumn>;20、xPos>;2000或xPos>;200&;xPos>;4000。你知道吗

有推荐的方法吗?还是一个包裹?你知道吗

<OwnCell cellRow="20" cellColumn="103" xPos="24901" widthPx="49" value="22" />
<OwnCell cellRow="20" cellColumn="104" xPos="25137" widthPx="49" value="25" />
<OwnCell cellRow="21" cellColumn="105" xPos="25373" widthPx="49" value="31" />
<OwnCell cellRow="20" cellColumn="106" xPos="25609" widthPx="49" value="28" />
<OwnCell cellRow="24" cellColumn="107" xPos="25845" widthPx="49" value="19" />
<OwnCell cellRow="20" cellColumn="108" xPos="26081" widthPx="49" value="19" />

谢谢你!你知道吗


Tags: 文件方法标记gtvalueampxposcellrow
1条回答
网友
1楼 · 发布于 2024-10-01 19:28:31

您可以按以下方式筛选您的文档

import xml.etree.ElementTree as xee
data='''\
<root>
  <OwnCell cellRow="20" cellColumn="103" xPos="24901" widthPx="49" value="22" />
<OwnCell cellRow="20" cellColumn="104" xPos="25137" widthPx="49" value="25" />
<OwnCell cellRow="21" cellColumn="105" xPos="25373" widthPx="49" value="31" />
<OwnCell cellRow="20" cellColumn="106" xPos="25609" widthPx="49" value="28" />
<OwnCell cellRow="24" cellColumn="107" xPos="25845" widthPx="49" value="19" />
<OwnCell cellRow="20" cellColumn="108" xPos="26081" widthPx="49" value="19" />
</root>
'''
doc=xee.fromstring(data)

for tag in doc.findall('OwnCell'):
    if int(tag.attrib['cellRow'])<=20: #add your filter condition here
        doc.remove(tag)
print(xee.tostring(doc))

首先找到要筛选的所有元素 然后添加条件以排除符合此条件的元素

相关问题 更多 >

    热门问题