在 python 中绘制 XML 文件中的变量

问题描述 投票:0回答:1

我有 XML 文件(请参阅附件)。

我制作了一个 python 脚本来尝试从 XML 中获取信息,以便稍后做一些绘图。

这段代码的目的是:

a.迭代 XML 文件以找到

</event>
,然后找到
</origin>
,然后找到
<quality>
到达
<azimuthalGap>348.000</azimuthalGap>

b.将每个 azimutalGap 保存在

gaps=[]
变量中

c.绘制 azimutalGap 的直方图。

到目前为止我尝试了代码:

import xml.etree.ElementTree as ET

def parse_azimuthal_gaps(xml_file):
    gaps = []

    # Parse the XML file
    tree = ET.parse(xml_file)
    root = tree.getroot()

    # Iterate through each event
    for event in root.findall(".//event"):
        origin = event.find(".//origin")
        if origin is not None:
            # Find the azimuthal gap (if present)
            azimuthal_gap = origin.find(".//quality/azimuthalGap")
            if azimuthal_gap is not None:
                gap_value = azimuthal_gap.text
                if gap_value is not None:
                    try:
                        gaps.append(float(gap_value))
                    except ValueError:
                        continue  # Skip if not a valid float

    return gaps

# Parse azimuthal gaps from the ISC XML file
gaps = parse_azimuthal_gaps("SCB_earthquakes.xml")
print("Azimuthal Gaps:", gaps[:10])  # Print the first 10 gaps for inspection

但我总是得到空间隙变量。

输入文件具有以下格式:

</event>
<event publicID="smi:ISC/evid=602401243">
  <preferredOriginID>smi:ISC/origid=601808754</preferredOriginID>
  <description>
    <text>Peru-Bolivia border region</text>
    <type>Flinn-Engdahl region</type>
  </description>
  <type>earthquake</type>
  <typeCertainty>known</typeCertainty>
  <comment>
    <text>Event reviewed by the ISC</text>
  </comment>
  <creationInfo>
    <agencyID>ISC</agencyID>
    <author>ISC</author>
  </creationInfo>
  <origin publicID="smi:ISC/origid=601808754">
    <time>
      <value>2011-01-23T09:13:37.30Z</value>
      <uncertainty>0.96</uncertainty>
    </time>
    <latitude>
      <value>-11.9240</value>
    </latitude>
    <longitude>
      <value>-68.9133</value>
    </longitude>
    <depth>
      <value>58000.0</value>
    </depth>
    <depthType>operator assigned</depthType>
    <quality>
      <usedPhaseCount>8</usedPhaseCount>
      <associatedStationCount>6</associatedStationCount>
      <standardError>0.4800</standardError>
      <azimuthalGap>353.000</azimuthalGap>
      <minimumDistance>4.260</minimumDistance>
      <maximumDistance>5.070</maximumDistance>
    </quality>
    <creationInfo>
      <author>SCB</author>
      <agencyID>SCB</agencyID>
    </creationInfo>
    <originUncertainty>
      <preferredDescription>uncertainty ellipse</preferredDescription>
      <minHorizontalUncertainty>20399.9996185303</minHorizontalUncertainty>
      <maxHorizontalUncertainty>96000</maxHorizontalUncertainty>
      <azimuthMaxHorizontalUncertainty>83.0</azimuthMaxHorizontalUncertainty>
    </originUncertainty>
    <arrival publicID="smi:ISC/pickid=637614753/hypid=601808754">
      <pickID>smi:ISC/pickid=637614753</pickID>
      <phase>Pn</phase>
      <azimuth>170.165</azimuth>
      <distance>4.404</distance>
      <timeResidual>3.6</timeResidual>
    </arrival>
    <arrival publicID="smi:ISC/pickid=637614754/hypid=601808754">
      <pickID>smi:ISC/pickid=637614754</pickID>
      <phase>Sn</phase>
      <azimuth>170.165</azimuth>
      <distance>4.404</distance>
      <timeResidual>4.6</timeResidual>
    </arrival>
  </origin>
  <pick publicID="smi:ISC/pickid=637614753">
    <time>
      <value>2011-01-23T09:14:46.10Z</value>
    </time>
    <waveformID networkCode="IR" stationCode="LPAZ"></waveformID>
    <onset>impulsive</onset>
    <polarity>positive</polarity>
    <phaseHint>Pn</phaseHint>
  </pick>
  <pick publicID="smi:ISC/pickid=637614754">
    <time>
      <value>2011-01-23T09:15:37.60Z</value>
    </time>
    <waveformID networkCode="IR" stationCode="LPAZ"></waveformID>
    <onset>emergent</onset>
    <phaseHint>Sn</phaseHint>
  </pick>
  <magnitude publicID="smi:ISC/magid=602398394">
    <mag>
      <value>3.80</value>
      <uncertainty>0.40</uncertainty>
    </mag>
    <type>Ml</type>
    <originID>smi:ISC/origid=601808754</originID>
    <stationCount>2</stationCount>
    <creationInfo>
      <author>SCB</author>
    </creationInfo>
  </magnitude>
  <preferredMagnitudeID>smi:ISC/magid=602398394</preferredMagnitudeID>

您有改进代码的想法吗?

托尼诺

python xml
1个回答
0
投票

如果您有事件列表,您可以创建感兴趣的列表:

import xml.etree.ElementTree as ET

root = ET.parse("mag.xml").getroot()

ag_list = root.findall(".//azimuthalGap")
print([x.text for x in ag_list])

输出:

['353.000', '453.000', …]
© www.soinside.com 2019 - 2024. All rights reserved.