首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >使用python从xml文件中读取某些信息

使用python从xml文件中读取某些信息
EN

Stack Overflow用户
提问于 2019-06-19 19:52:57
回答 1查看 42关注 0票数 0

我是xml的新手,对python有一点经验。我正在尝试使用python的xml.etree.ElementTree模块来解析xml文件。使用print(info.text),它可以打印出xml文件中的所有元素,如WIL-RT2Rtr_Cisco…,但我只想检索WIL-RT2,然后基于WIL-RT2,我可以返回Rtr_Cisco。以下是我的xml文件的一部分:

代码语言:javascript
复制
<?xml version="1.0" encoding="ISO-8859-1"?>
<model-response-list xmlns="http://www.ca.com/spectrum/restful/schema/response" total-models="115" throttle="115" error="EndOfResults">
<model-responses>
<model mh="0x700e8f">
<attribute id="0x1006e">WIL-RT2</attribute>
<attribute id="0x10000">Rtr_Cisco</attribute>
<attribute id="0x12d7f">172.20.111.252</attribute>
<attribute id="0x12bfb">NAT,192.168.249.150</attribute>
<attribute id="0x100ae" error="NoSuchAttribute"/>
<attribute id="0x100af" error="NoSuchAttribute"/>
</model>
</model-responses>
</model-response-list>

下面是我的python代码

代码语言:javascript
复制
for item in root.findall("{http://www.ca.com/spectrum/restful/schema/response}model-responses"):
     #print(item.findall('{http://www.ca.com/spectrum/restful/schema/response}model'))
     for child in item.findall('{http://www.ca.com/spectrum/restful/schema/response}model'):
         #print(child.attrib, child.tag)
         for info in child.findall('{http://www.ca.com/spectrum/restful/schema/response}attribute'):
               print(info.tag, info.attrib)
               print(info.text)
EN

回答 1

Stack Overflow用户

发布于 2019-06-20 08:31:36

有一种方法可以获取文本。下面是代码打印(info.findtext(“@id=‘0x1006e’”))

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/56667056

复制
相关文章

相似问题

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