【问题标题】:Extract data from multiple .xml files in multiple folders从多个文件夹中的多个 .xml 文件中提取数据
【发布时间】:2020-11-20 13:14:58
【问题描述】:

我不确定我在这里做什么。我正在尝试从具有相同名称但存储在不同文件夹中的 XML 文件中提取数据。运行它时没有错误,但我没有返回数据或没有打印任何内容。有人可以帮我理解我在这里做错了什么吗?

import glob
import os
from lxml import etree

dir = 'C:/Users/franc/AppData/Local/Temp/LSTO/'
for file in glob.iglob(os.path.join(dir, 'summary.xml')):
   with open(file) as f:
      data = etree.parse(f)
      root = data.root()
      for temp in root.iter('Summary'):
         print(temp.attrib)
         temp_data = '/summary/@maxTemperature'

         print(temp_data)

我正在尝试从此 XML 文件中提取 maxTemperature 数据:

<?xml version="1.0"?>
<Summary xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
    <misc partMass="4.8589068478323991" name="LSTO, Boron Nitride + 1 other"/>
    <summary maxTemperature="150.679214" minTemperature="20" multiMode="Discrete" volume="1531.5476989044932" firstMaterialFraction="0.4078046203055925" countMaterials="2" maxElementStress="-3.40282347E+38" minElementStress="3.40282347E+38" maxStress="-3.40282347E+38" minStress="3.40282347E+38" maxDisplacementLength="0">
        <minDisplacement z="3.40282347E+38" y="3.40282347E+38" x="3.40282347E+38"/>
        <maxDisplacement z="-3.40282347E+38" y="-3.40282347E+38" x="-3.40282347E+38"/>
        <cauchyStressMin>
            <normal z="3.40282347E+38" y="3.40282347E+38" x="3.40282347E+38"/>
            <shear z="3.40282347E+38" y="3.40282347E+38" x="3.40282347E+38"/>
        </cauchyStressMin>
        <cauchyStressMax>
            <normal z="-3.40282347E+38" y="-3.40282347E+38" x="-3.40282347E+38"/>
            <shear z="-3.40282347E+38" y="-3.40282347E+38" x="-3.40282347E+38"/>
        </cauchyStressMax>
    </summary>
    <materials>
        <material name="Boron Nitride" density="2" id="1">
            <matrix density="2" CTE="0" Gzp="24550" nuz="0.23999999463558197" Ez="27000" nu="0.23999999463558197" Ep="94500"/>
            <properties>
                <textProperty value="Yes" key="Soluble Support Available?"/>
                <numericProperty value="4.5" key="Elongation at Break" costType="Unknown" unit="%"/>
                <numericProperty value="124" key="Ultimate Tensile Strength" costType="Unknown" unit="MPa"/>
                <numericProperty value="20" key="Infill Density" costType="Unknown" unit="%"/>
                <textProperty value="Triangles" key="Infill Pattern"/>
                <numericArrayProperty key="Thermal Matrix" unit="W/(mmC)">0.45 0 0 0 0.45 0 0 0 0.03</numericArrayProperty>
                <textProperty value="Boron Nitride" key="Material Name"/>
                <textProperty value="Metal" key="General Material Type"/>
                <textProperty value="PLA" key="Specific Material Type"/>
            </properties>
        </material>
        <material name="Alumina" density="3.98" id="2">
            <matrix density="3.9800000190734863" CTE="0" Gzp="126500" nuz="0.27000001072883606" Ez="314000" nu="0.27000001072883606" Ep="314000"/>
            <properties>
                <textProperty value="Yes" key="Soluble Support Available?"/>
                <numericProperty value="4.5" key="Elongation at Break" costType="Unknown" unit="%"/>
                <numericProperty value="124" key="Ultimate Tensile Strength" costType="Unknown" unit="MPa"/>
                <numericProperty value="80" key="Infill Density" costType="Unknown" unit="%"/>
                <textProperty value="Triangles" key="Infill Pattern"/>
                <numericArrayProperty key="Thermal Matrix" unit="W/(mmC)">0.02525 0 0 0 0.02525 0 0 0 0.02525</numericArrayProperty>
                <textProperty value="Alumina" key="Material Name"/>
                <textProperty value="Ceramic" key="General Material Type"/>
                <textProperty value="PLA" key="Specific Material Type"/>
            </properties>
        </material>
    </materials>
</Summary>

【问题讨论】:

    标签: python xml xml-parsing lxml


    【解决方案1】:

    您正在迭代 Summary 元素,但您想要的元素是 summary(注意大小写的区别?)。

    解析前也不需要打开xml文件。

    我也不确定temp_data 字符串应该做什么。

    您也没有遍历多个文件夹;您正在处理单个目录中的单个文件。您打算如何提供目录?目录列表?

    这是一个至少可以让您从单个 XML 文件中提取 @maxTemperature 的示例...

    from lxml import etree
    
    tree = etree.parse("summary.xml")
    
    for summary in tree.iter("summary"):
        print(summary.get("maxTemperature", "Max temperature not found."))
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-02-05
      • 1970-01-01
      • 1970-01-01
      • 2019-02-17
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多