首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何从XML文件中获取数据?

如何从XML文件中获取数据?
EN

Stack Overflow用户
提问于 2019-07-29 18:01:37
回答 2查看 2.4K关注 0票数 3

我正在尝试从XML文件中获取数据,我尝试了其中的一些代码,但我无法获得正确的结果。

代码语言:javascript
复制
    import xml.etree.ElementTree as etree
    XmlD=etree.parse("MVI_20011.xml")
    root=XmlD.getroot()
    for child in root:
        print(child.get("density"),child.get("num"))
        for children in child:
            print(children.text)

下面是我的XML文件中的一些行:

代码语言:javascript
复制
   <?xml version="1.0" encoding="utf-8"?>
   <sequence name="MVI_20011">
      <sequence_attribute camera_state="unstable" sence_weather="sunny"/>
      <ignored_region>
         <box left="778.75" top="24.75" width="181.75" height="63.5"/>
         <box left="930.75" top="94.75" width="29.75" height="33.5"/>
         <box left="523.75" top="13.75" width="178.5" height="37.5"/>
         <box left="207.75" top="34.75" width="270.5" height="94.5"/>
         <box left="182.75" top="131.75" width="145.5" height="117.5"/>
         <box left="0.5" top="231.75" width="88.75" height="121.5"/>
         <box left="123.75" top="151.75" width="57.5" height="97.5"/>
      </ignored_region>
      <frame density="7" num="1">
         <target_list>
            <target id="1">
               <box left="592.75" top="378.8" width="160.05" height="162.2"/>
               <attribute orientation="18.488" speed="6.859" trajectory_length="5" truncation_ratio="0.1" vehicle_type="car"/>
            </target>
            <target id="2">
               <box left="557.65" top="120.98" width="47.2" height="43.06"/>
               <attribute orientation="19.398" speed="1.5055" trajectory_length="72" truncation_ratio="0" vehicle_type="car"/>
            </target>
            <target id="3">
               <box left="545.2" top="88.27" width="35.25" height="30.08"/>
               <attribute orientation="2.7525" speed="0.5206" trajectory_length="105" truncation_ratio="0" vehicle_type="car"/>
               <occlusion>
                  <region_overlap left="553" top="88.27" width="27.45" height="1.52" occlusion_id="5" occlusion_status="1"/>
               </occlusion>
            </target>
            <target id="4">
               <box left="508.35" top="67.5" width="28.0" height="25.925"/>
               <attribute orientation="349.06" speed="0.52707" trajectory_length="132" truncation_ratio="0" vehicle_type="car"/>
            </target>
            <target id="5">
               <box left="553" top="70.095" width="29.55" height="19.695"/>
               <attribute orientation="58.543" speed="0.49822" trajectory_length="151" truncation_ratio="0" vehicle_type="car"/>
            </target>
            <target id="6">
               <box left="731.1" top="114.23" width="52.4" height="39.95"/>
               <attribute orientation="227.87" speed="2.2585" trajectory_length="91" truncation_ratio="0" vehicle_type="car"/>
            </target>
            <target id="7">
               <box left="902.15" top="250.12" width="58.85" height="107.99"/>
               <attribute orientation="197.29" speed="12.115" trajectory_length="157" truncation_ratio="0.3583" vehicle_type="car"/>
            </target>
         </target_list>
      </frame>

我想要获取第一帧的每一帧的数据:

代码语言:javascript
复制
(7,1) 
592.75 378.8 160.05 162.2
557.65 120.98 47.2 43.06

另一个盒子,我希望你能理解我的问题。谢谢。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2019-07-29 18:23:06

使用以下方法:

代码语言:javascript
复制
import xml.etree.ElementTree as ET

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

for frame in root.findall('frame'):
    print((frame.get('density'), frame.get('num')))
    for box in frame.findall('.//box'):
        print(*box.attrib.values())

输出:

代码语言:javascript
复制
('7', '1')
592.75 378.8 160.05 162.2
557.65 120.98 47.2 43.06
545.2 88.27 35.25 30.08
508.35 67.5 28.0 25.925
553 70.095 29.55 19.695
731.1 114.23 52.4 39.95
902.15 250.12 58.85 107.99
票数 3
EN

Stack Overflow用户

发布于 2019-07-29 18:19:02

您将获得标记文本,但您需要属性值。

下面是一个示例:

代码语言:javascript
复制
XmlD.find(('.//frame')).attrib['density']

# this should return '7'
代码语言:javascript
复制
for key, value in XmlD.find(('.//frame')).items():
    print(key, value)

# this should return
# density 7
# num 1
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/57251439

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档