python - python:用没有类的xmlns解析xml文件
问题描述
下面是 XML 文件:
<Schedule xmlns="http://xmlns.xyz.com/fal/downtimeschedule/V1.0">
<Downtime end="20181020000" id="10001197610_20181027000_201810000" mode="cold" start="20181020000"/>
<PODS>
<POD DC="US - Washing" deferUpgrade="true" name="ABCD" patching="Production" EndTime="20181028040000">
<CR id="12345"/>
<CR id="12346"/>
<CR id="123"/>
</POD>
<POD DC="US - Washing" deferUpgrade="true" name="ABCD-TEST" patching="Production" EndTime="20181028040000">
<CR id="12345"/>
<CR id="12346"/>
<CR id="123"/>
</POD>
</PODS>
我想在带有列的 csv 文件中提供输出
1> POD(name)
2> POD(DC)
3> POD(deferUpgrade)
有人可以在这里帮忙。是否可以使用库 xml.etree.ElementTree ,因为我在生产环境中有这个。
下面是代码。
#!/usr/bin/python
import os
import pandas as pd
import requests
import xml.etree.ElementTree as ET
tree = ET.parse('schedule.xml')
root = tree.getroot()
print root
print root.findall('PODS')
for pods in root.iter('Schedule'):
for pod in pods.iter('POD'):
print pod.get('name')
输出:
<Element '{http://xmlns.oracle.com/falcm/flo/downtimeschedule/V1.0}Schedule' at 0x2ae6ed0>
[]
解决方案
from xml.dom import minidom
def parseXml(filePath):
xmldoc = minidom.parse(filePath)
PODs= xmldoc.getElementsByTagName("POD")
name=[]
DC=[]
deferUpgrade=[]
for pod in PODs:
if pod.hasAttribute("name") and pod.hasAttribute("DC") and pod.hasAttribute("deferUpgrade"):
name.append(pod.getAttribute("name"))
DC.append(pod.getAttribute("DC"))
deferUpgrade.append(pod.getAttribute("deferUpgrade"))
return(name,DC,deferUpgrade)
您可以使用 name,DC,deferUpgrade 来制作您的 CSV 文件
推荐阅读
- python-3.x - 将抓取的数据导出到 CSV
- python - 如何在 NMF Python 上执行交叉验证
- java - ScheduledExecutorService 及其调度方法。这个怎么运作?
- mongodb - 在 MongoDB 聚合 $addFields 中按条件选择数组元素
- azure - Azure DataLake (ADLS) 批量下载错误请求
- excel - 如何获取 VBA 代码以检查单元格值是否与列标题匹配并从同一行中的另一个单元格返回值?
- python - Numpy lomax 均值函数返回“inf”而不是值
- date - 如何批量比较两个日期?
- amazon-web-services - 使用 AWS CLI 查询文件夹内的文件名?
- css - 汉堡菜单不适用于样式化组件