python - 将 REST API 中的特定部分保存到列表中
问题描述
我目前正在编写一个脚本,该脚本从我们的 Cisco Prime 基础设施中获取网络设备的完整列表,然后将这些设备添加到我们的 Cisco ISE 安装中。
我们 Cisco Prime 基础设施的输出数据超过 25000 行文本。
示例输出:
<entity dtoType="devicesDTO" type="Devices" url="https://X.X.X.X/webacs/api/v4/data/Devices/7860155308">
<devicesDTO displayName="7860155308" id="7860155308">
<collectionDetail><status><general code="SUCCESS"/></status></collectionDetail>
<collectionTime>2019-06-28T01:02:32.494+02:00</collectionTime>
<creationTime>2018-10-05T08:45:01.461+02:00</creationTime>
<deviceId>7849390591</deviceId>
<deviceName>008-HX.TEST.SWITCH-CORE-01</deviceName>
<deviceType>Cisco Catalyst38xx stack-able ethernet switch</deviceType>
<ipAddress>172.29.0.254</ipAddress>
<location>Test Location</location>
<managementStatus>MANAGED_AND_SYNCHRONIZED</managementStatus>
<productFamily>Switches and Hubs</productFamily>
<reachability>REACHABLE</reachability>
<softwareType>IOS-XE</softwareType>
<softwareVersion>16.6.5</softwareVersion>
<adminStatus>MANAGED</adminStatus>
<collectionStatus>COMPLETED</collectionStatus>
<manufacturerPartNrs>
<manufacturerPartNr>
<partNumber>WS-C3850-12XS-E</partNumber>
</manufacturerPartNr>
</manufacturerPartNrs>
</devicesDTO>
</entity>
<entity dtoType="devicesDTO" type="Devices" url="https://X.X.X.X/webacs/api/v4/data/Devices/7605631035">
<devicesDTO displayName="7605631035" id="7605631035">
<collectionDetail><status><general code="SUCCESS"/></status></collectionDetail>
<collectionTime>2019-06-28T00:45:33.177+02:00</collectionTime>
<creationTime>2018-09-04T01:48:15.790+02:00</creationTime>
<deviceId>7598542980</deviceId>
<deviceName>008-HX.TEST.SWITCH-UA-01.Vejle.dk</deviceName>
<deviceType>Cisco Catalyst 29xx Stack-able Ethernet Switch</deviceType>
<ipAddress>172.29.0.2</ipAddress>
<location>Test Location</location>
<managementStatus>MANAGED_AND_SYNCHRONIZED</managementStatus>
<productFamily>Switches and Hubs</productFamily>
<reachability>REACHABLE</reachability>
<softwareType>IOS</softwareType>
<softwareVersion>15.2(2)E9</softwareVersion>
<adminStatus>MANAGED</adminStatus>
<collectionStatus>COMPLETED</collectionStatus>
<manufacturerPartNrs>
<manufacturerPartNr>
<partNumber>WS-C2960X-48FPD-L</partNumber>
</manufacturerPartNr>
</manufacturerPartNrs>
</devicesDTO>
</entity>
<entity dtoType="devicesDTO" type="Devices" url="https://X.X.X.X/webacs/api/v4/data/Devices/7605631034">
<devicesDTO displayName="7605631034" id="7605631034">
<collectionDetail><status><general code="SUCCESS"/></status></collectionDetail>
<collectionTime>2019-06-28T00:46:35.393+02:00</collectionTime>
<creationTime>2018-09-04T01:48:00.776+02:00</creationTime>
<deviceId>7598542967</deviceId>
<deviceName>008-HX.TEST.SWITCH-UA-02.Vejle.dk</deviceName>
<deviceType>Cisco Catalyst 29xx Stack-able Ethernet Switch</deviceType>
<ipAddress>172.29.0.3</ipAddress>
<location>Test Location</location>
<managementStatus>MANAGED_AND_SYNCHRONIZED</managementStatus>
<productFamily>Switches and Hubs</productFamily>
<reachability>REACHABLE</reachability>
<softwareType>IOS</softwareType>
<softwareVersion>15.2(2)E9</softwareVersion>
<adminStatus>MANAGED</adminStatus>
<collectionStatus>COMPLETED</collectionStatus>
<manufacturerPartNrs>
<manufacturerPartNr>
<partNumber>WS-C2960X-48FPD-L</partNumber>
</manufacturerPartNr>
</manufacturerPartNrs>
</devicesDTO>
</entity>
<entity dtoType="devicesDTO" type="Devices" url="https://X.X.X.X/webacs/api/v4/data/Devices/7605631033">
<devicesDTO displayName="7605631033" id="7605631033">
<collectionDetail><status><general code="SUCCESS"/></status></collectionDetail>
<collectionTime>2019-06-28T00:47:01.412+02:00</collectionTime>
<creationTime>2018-09-04T01:47:45.817+02:00</creationTime>
<deviceId>7598542954</deviceId>
<deviceName>008-UX1.TEST.SWITCH-UA-01.Vejle.dk</deviceName>
<deviceType>Cisco Catalyst 29xx Stack-able Ethernet Switch</deviceType>
<ipAddress>172.29.0.4</ipAddress>
<location>Test Location</location>
<managementStatus>MANAGED_AND_SYNCHRONIZED</managementStatus>
<productFamily>Switches and Hubs</productFamily>
<reachability>REACHABLE</reachability>
<softwareType>IOS</softwareType>
<softwareVersion>15.2(2)E9</softwareVersion>
<adminStatus>MANAGED</adminStatus>
<collectionStatus>COMPLETED</collectionStatus>
<manufacturerPartNrs>
<manufacturerPartNr>
<partNumber>WS-C2960X-48FPD-L</partNumber>
</manufacturerPartNr>
</manufacturerPartNrs>
</devicesDTO>
</entity>
<entity dtoType="devicesDTO" type="Devices" url="https://X.X.X.X/webacs/api/v4/data/Devices/7605631038">
<devicesDTO displayName="7605631038" id="7605631038">
<collectionDetail><status><general code="SUCCESS"/></status></collectionDetail>
<collectionTime>2019-06-28T00:47:25.438+02:00</collectionTime>
<creationTime>2018-09-04T01:49:01.038+02:00</creationTime>
<deviceId>7598543019</deviceId>
<deviceName>008-UX2.TEST.SWITCH-UA-01.Vejle.dk</deviceName>
<deviceType>Cisco Catalyst 35xx Stack-able Ethernet Switch</deviceType>
<ipAddress>172.29.0.5</ipAddress>
<location>Test Location</location>
<managementStatus>MANAGED_AND_SYNCHRONIZED</managementStatus>
<productFamily>Switches and Hubs</productFamily>
<reachability>REACHABLE</reachability>
<softwareType>IOS</softwareType>
<softwareVersion>15.2(4)E7</softwareVersion>
<adminStatus>MANAGED</adminStatus>
<collectionStatus>COMPLETED</collectionStatus>
<manufacturerPartNrs>
<manufacturerPartNr>
<partNumber>WS-C3560CX-12PD-S</partNumber>
</manufacturerPartNr>
</manufacturerPartNrs>
</devicesDTO>
</entity>
<entity dtoType="devicesDTO" type="Devices" url="https://X.X.X.X/webacs/api/v4/data/Devices/7605631037">
<devicesDTO displayName="7605631037" id="7605631037">
<collectionDetail><status><general code="SUCCESS"/></status></collectionDetail>
<collectionTime>2019-06-28T00:45:32.775+02:00</collectionTime>
<creationTime>2018-09-04T01:48:45.760+02:00</creationTime>
<deviceId>7598543006</deviceId>
<deviceName>008-UX3.TEST.SWITCH-UA-01.Vejle.dk</deviceName>
<deviceType>Cisco Catalyst 29xx Stack-able Ethernet Switch</deviceType>
<ipAddress>172.29.0.6</ipAddress>
<location>Test Location</location>
<managementStatus>MANAGED_AND_SYNCHRONIZED</managementStatus>
<productFamily>Switches and Hubs</productFamily>
<reachability>REACHABLE</reachability>
<softwareType>IOS</softwareType>
<softwareVersion>15.2(2)E9</softwareVersion>
<adminStatus>MANAGED</adminStatus>
<collectionStatus>COMPLETED</collectionStatus>
<manufacturerPartNrs>
<manufacturerPartNr>
<partNumber>WS-C2960X-24PD-L</partNumber>
</manufacturerPartNr>
</manufacturerPartNrs>
</devicesDTO>
</entity>
现在我只想将以实体开头并以 /entity 结尾的行保存到列表中,并且每个部分都应该是自己的列表。
例子:
[['<entity dtoType="devicesDTO" type="Devices" url="https://X.X.X.X/webacs/api/v4/data/Devices/7860155308">', '...truncated...', '</entity>'], ['<entity dtoType="devicesDTO" type="Devices" url="https://X.X.X.X/webacs/api/v4/data/Devices/7605631035">', '...truncated...', '</entity>']]
我当前的代码:
#!/usr/bin/python
# coding=utf-8
import sys
import requests
import xml.etree.ElementTree as ET
response = requests.get('https://X.X.X.X/webacs/api/v4/data/Devices?.full=true&.sort=deviceName', verify=False)
output = response.text
data = []
for line in output:
data.append(line)
但它给出了奇怪的结果。我目前无法理解如何解决这个问题。一些帮助将不胜感激。
编辑:
我可以看到每行末尾都有一个\n 。如果我这样做:
output = output.split("\n")
我得到一个包含所有数据的列表。
解决方案
您可以采取两种方法:
第一种方法:逐行如果您想让代码逐行运行,您可以使用regex检查该行是否符合您的要求:
for line in output:
if re.match(r'entity.*/entity', line):
print("It's a match")
这将匹配您具有“任何文本(如<)实体任何文本/实体任何文本(如>)”的每一行但是,这只会捕获单行实体。或者,您可以找到带有开始标签和开始标签的行,并在其间捕获内容:
for line in output:
if re.match(r'entity', line):
if re.match(r'/entity', line):
print("End of block")
else:
print("Start of block")
第二种方法:XML
如果您想真正将其视为 XML,不仅要查找单行实体对象,还可以使用xml.etree.ElementTree并按照此处所述运行递归 findall来查找每个实体元素。
推荐阅读
- git - 如何在 .git/hooks/pre-commit 文件上提交我的更改
- git - Heroku 登录在 git bash 中无法正常工作
- algorithm - 如何使用优先队列?
- python - 无法将视图从主 Django 项目文件夹导入应用程序 views.py
- r - 尝试在 R 中创建带有箭头的图形,但不能将离散值应用于连续比例
- css - 下拉菜单在悬停时没有延迟。“引导程序4”
- java - 向序列化 JSON 对象添加额外字段
- sql - 从组中选择 1 个字段
- javascript - 从 Javascript 发送 JSON 数组并在 php 中接收值
- node.js - webContents.send 和 ipcRenderer.on 不工作