dimanche 29 décembre 2019

Is there a way to filter xml by an id attribute in elementtree using python

I am new to python and elementtree in particular and am trying to filter some information from an xml. I have managed to locate the information I need fo every TX id (coordinates), however I want to filter this to just Tx id "TxA". Ive included a section of the xml file and the code below with some comments to help show the problem. Any help or guidance is greatly appreciated.

All lists were previously set up (hence appending) Sections commented, do work for all Tx ids however I am now going back to try and filter Subelm.attrib gives the Tx id. Ive shown two attempts ive made in the code on lines 5-8

<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>

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)

Aucun commentaire:

Enregistrer un commentaire