用elemen搜索Python中的嵌套XML

2024-05-28 18:55:11 发布

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

我有一个嵌套的XML文件,我希望能够搜索并存储一些元素到一个数组中。在

我不知道怎么做。。。。在

XML文件如下所示

<?xml version="1.0" encoding="ISO-8859-1"?>
<modeling>
 <generator>
  <i name="program" type="string">vasp </i>
  <i name="version" type="string">5.4.1  </i>
  <i name="subversion" type="string">24Jun15 (build Dec 17 2015 12:51:06) complex                          parallel </i>
  <i name="platform" type="string">IFC91_ompi </i>
  <i name="date" type="string">2016 04 09 </i>
  <i name="time" type="string">21:14:15 </i>
 </generator>
 <incar>
  <i type="string" name="SYSTEM"> LGPS</i>
  <i type="int" name="ISTART">     1</i>
  <i type="string" name="PREC">low (precision level)</i>
  <i type="string" name="ALGO"> Very Fast   (Elect. algorithm for MD)</i>
  <i type="logical" name="ADDGRID"> T  </i>
  <i type="int" name="ISPIN">     1</i>
  <i type="int" name="MAXMIX">    40</i>
  <i type="int" name="INIWAV">     1</i>
  <i type="int" name="NELM">    20</i>
  <i type="int" name="IBRION">    -1</i>
  <i name="EDIFF">      0.00005000</i>
  <i name="EDIFFG">     -0.01000000</i>
  <i type="int" name="NSW">     0</i>
  <i type="int" name="ISIF">     2</i>
  <i type="int" name="ISYM">     0</i>
  <i type="int" name="NBLOCK">    20</i>
  <i name="ENMAX">    400.00000000</i>
  <i name="POTIM">      2.00000000</i>
  <i name="TEBEG">   1500.00000000</i>
  <i name="TEEND">   1500.00000000</i>
  <i name="SMASS">      1.00000000</i>
  <i type="string" name="LREAL"> Auto      (Projection operators: automa</i>
  <v name="ROPT">     -0.00100000     -0.00100000     -0.00100000     -0.00100000</v>
  <i type="int" name="ISMEAR">     0</i>
  <i type="int" name="NWRITE">     2</i>
  <i type="logical" name="LCORR"> F  </i>
  <i type="int" name="LMAXMIX">     4</i>
  <i type="logical" name="LORBIT"> F  </i>
  <i type="logical" name="LASPH"> T  </i>
  <i type="int" name="ICORELEVEL">     1</i>

。。。。。。。 它最终到达这个数组,我想使用elementtree将它从XML文件提取到numpy数组。在

^{pr2}$

任何建议都会有帮助。在

谢谢!在


Tags: 文件name元素stringversiontypeiso数组
1条回答
网友
1楼 · 发布于 2024-05-28 18:55:11
import xml.etree.ElementTree as ET
import numpy as np

tree = ET.parse('input.xml')
root = tree.getroot()

a = np.array(
    [
        v.text.split()
        for v in root.findall(".//calculation/varray/v")
    ],
    dtype='float'
)

print a

相关问题 更多 >

    热门问题