有没有一种方法可以使用 python 通过 elementtree 中的 id 属性过滤 xml
Is there a way to filter xml by an id attribute in elementtree using python
我是 python 的新手,尤其是 elementtree,我正在尝试从 xml 中过滤一些信息。我已经设法找到每个 TX id(坐标)所需的信息,但是我想将其过滤为仅 Tx id "TxA"。我包含了 xml 文件的一部分和下面的代码以及一些注释以帮助显示问题。非常感谢任何帮助或指导。
所有列表都是以前设置的(因此附加)
评论部分,对所有 Tx id 都有效,但是我现在回去尝试过滤
Subelm.attrib 给出 Tx id。我在第 5-8 行的代码中展示了我所做的两次尝试
XML的一部分:
<TX id="TxA">
<Tx_WGS84_Longitude>-105.0846057</Tx_WGS84_Longitude>
<Tx_WGS84_Latitude>42.9565772</Tx_WGS84_Latitude>
<Tx_Easting>678133.8818</Tx_Easting>
<Tx_Northing>895120.939</Tx_Northing>
<Channel id="3">
<Ant_Config> =</Ant_Config>
<Ant_name>Tx_ant_name1</Ant_name>
<Ant_Electrode_1>TxAEL1<Ant_Easting>678135.1069</Ant_Easting><Ant_Northing>895248.2057</Ant_Northing></Ant_Electrode_1>
<Ant_Electrode_2>TxAEL2<Ant_Easting>678137.0213</Ant_Easting><Ant_Northing>891059.2502</Ant_Northing></Ant_Electrode_2>
</Channel>
<Channel id="1">
<Ant_Config> =</Ant_Config>
<Ant_name>Tx_ant_name2</Ant_name>
<Ant_Electrode_1>TxAEL1<Ant_Easting>678135.1069</Ant_Easting><Ant_Northing>895248.2057</Ant_Northing></Ant_Electrode_1>
<Ant_Electrode_2>TxAEL2<Ant_Easting>678137.0213</Ant_Easting><Ant_Northing>891059.2502</Ant_Northing></Ant_Electrode_2>
</Channel>
</TX>
<TX id="TxB">
<Tx_WGS84_Longitude>-105.08459550832</Tx_WGS84_Longitude>
<Tx_WGS84_Latitude>42.9506068474998</Tx_WGS84_Latitude>
<Tx_Easting>678135.4896</Tx_Easting>
<Tx_Northing>893006.9206</Tx_Northing>
<Channel id="3">
<Ant_Config> =</Ant_Config>
<Ant_name>Tx_ant_name1</Ant_name>
<Ant_Electrode_1>TxBEL1<Ant_Easting>678135.6131</Ant_Easting><Ant_Northing>893055.2569</Ant_Northing></Ant_Electrode_1>
<Ant_Electrode_2>TxBEL2<Ant_Easting>678138.3127</Ant_Easting><Ant_Northing>888854.3852</Ant_Northing></Ant_Electrode_2>
</Channel>
<Channel id="1">
<Ant_Config> =</Ant_Config>
<Ant_name>Tx_ant_name2</Ant_name>
<Ant_Electrode_1>TxBEL1<Ant_Easting>678135.6131</Ant_Easting><Ant_Northing>893055.2569</Ant_Northing></Ant_Electrode_1>
<Ant_Electrode_2>TxBEL2<Ant_Easting>678138.3127</Ant_Easting><Ant_Northing>888854.3852</Ant_Northing></Ant_Electrode_2>
</Channel>
部分Python代码:
for child in root:
if child.tag=='Layout':
for subelm in child:
if subelm.tag=='TX':
for name in subelm.iter('TxA'):
print (subelm.attrib)
if ('id' in subelm.attrib.text):
print (subelm.attrib.text)
for channel in subelm:
for electrode in channel:
for electrode1 in electrode.iter('Ant_Electrode_1'):
for electrode1 in electrode.iter('Ant_Easting'):
x1t.append(electrode1.text)
for electrode1 in electrode.iter('Ant_Northing'):
y1t.append(electrode1.text)
for electrode2 in electrode.iter('Ant_Electrode_2'):
for electrode2 in electrode.iter('Ant_Easting'):
x2t.append(electrode2.text)
for electrode2 in electrode.iter('Ant_Northing'):
y2t.append(electrode2.text)
您可以只使用 xpath 表达式:
>>> from lxml import etree
>>> with open('data.xml') as fd:
... doc = etree.parse(fd)
...
>>> matches = doc.xpath('//TX[@id="TxA"]')
>>> matches
[<Element TX at 0x7fbfdbf50370>]
>>> matches[0].find('Tx_WGS84_Longitude').text
'-105.0846057'
我是 python 的新手,尤其是 elementtree,我正在尝试从 xml 中过滤一些信息。我已经设法找到每个 TX id(坐标)所需的信息,但是我想将其过滤为仅 Tx id "TxA"。我包含了 xml 文件的一部分和下面的代码以及一些注释以帮助显示问题。非常感谢任何帮助或指导。
所有列表都是以前设置的(因此附加) 评论部分,对所有 Tx id 都有效,但是我现在回去尝试过滤 Subelm.attrib 给出 Tx id。我在第 5-8 行的代码中展示了我所做的两次尝试
XML的一部分:
<TX id="TxA">
<Tx_WGS84_Longitude>-105.0846057</Tx_WGS84_Longitude>
<Tx_WGS84_Latitude>42.9565772</Tx_WGS84_Latitude>
<Tx_Easting>678133.8818</Tx_Easting>
<Tx_Northing>895120.939</Tx_Northing>
<Channel id="3">
<Ant_Config> =</Ant_Config>
<Ant_name>Tx_ant_name1</Ant_name>
<Ant_Electrode_1>TxAEL1<Ant_Easting>678135.1069</Ant_Easting><Ant_Northing>895248.2057</Ant_Northing></Ant_Electrode_1>
<Ant_Electrode_2>TxAEL2<Ant_Easting>678137.0213</Ant_Easting><Ant_Northing>891059.2502</Ant_Northing></Ant_Electrode_2>
</Channel>
<Channel id="1">
<Ant_Config> =</Ant_Config>
<Ant_name>Tx_ant_name2</Ant_name>
<Ant_Electrode_1>TxAEL1<Ant_Easting>678135.1069</Ant_Easting><Ant_Northing>895248.2057</Ant_Northing></Ant_Electrode_1>
<Ant_Electrode_2>TxAEL2<Ant_Easting>678137.0213</Ant_Easting><Ant_Northing>891059.2502</Ant_Northing></Ant_Electrode_2>
</Channel>
</TX>
<TX id="TxB">
<Tx_WGS84_Longitude>-105.08459550832</Tx_WGS84_Longitude>
<Tx_WGS84_Latitude>42.9506068474998</Tx_WGS84_Latitude>
<Tx_Easting>678135.4896</Tx_Easting>
<Tx_Northing>893006.9206</Tx_Northing>
<Channel id="3">
<Ant_Config> =</Ant_Config>
<Ant_name>Tx_ant_name1</Ant_name>
<Ant_Electrode_1>TxBEL1<Ant_Easting>678135.6131</Ant_Easting><Ant_Northing>893055.2569</Ant_Northing></Ant_Electrode_1>
<Ant_Electrode_2>TxBEL2<Ant_Easting>678138.3127</Ant_Easting><Ant_Northing>888854.3852</Ant_Northing></Ant_Electrode_2>
</Channel>
<Channel id="1">
<Ant_Config> =</Ant_Config>
<Ant_name>Tx_ant_name2</Ant_name>
<Ant_Electrode_1>TxBEL1<Ant_Easting>678135.6131</Ant_Easting><Ant_Northing>893055.2569</Ant_Northing></Ant_Electrode_1>
<Ant_Electrode_2>TxBEL2<Ant_Easting>678138.3127</Ant_Easting><Ant_Northing>888854.3852</Ant_Northing></Ant_Electrode_2>
</Channel>
部分Python代码:
for child in root:
if child.tag=='Layout':
for subelm in child:
if subelm.tag=='TX':
for name in subelm.iter('TxA'):
print (subelm.attrib)
if ('id' in subelm.attrib.text):
print (subelm.attrib.text)
for channel in subelm:
for electrode in channel:
for electrode1 in electrode.iter('Ant_Electrode_1'):
for electrode1 in electrode.iter('Ant_Easting'):
x1t.append(electrode1.text)
for electrode1 in electrode.iter('Ant_Northing'):
y1t.append(electrode1.text)
for electrode2 in electrode.iter('Ant_Electrode_2'):
for electrode2 in electrode.iter('Ant_Easting'):
x2t.append(electrode2.text)
for electrode2 in electrode.iter('Ant_Northing'):
y2t.append(electrode2.text)
您可以只使用 xpath 表达式:
>>> from lxml import etree
>>> with open('data.xml') as fd:
... doc = etree.parse(fd)
...
>>> matches = doc.xpath('//TX[@id="TxA"]')
>>> matches
[<Element TX at 0x7fbfdbf50370>]
>>> matches[0].find('Tx_WGS84_Longitude').text
'-105.0846057'