gpt4 book ai didi

python - 你如何在python中将xml文件转换为数据框或csv输出

转载 作者:太空狗 更新时间:2023-10-30 00:09:03 25 4
gpt4 key购买 nike

我有这个 xml 文件,它想将内容转换为 python 中 csv 文件的数据框:

<?xml version="1.0" encoding="utf-8"?>
<dashboardreport name="jvm_report" version="7.0.21.1017" reportdate="2018-08-08T10:37:01.510-04:00" description="">
<source name="CORP_GTM">
<filters summary="from Jul-30 23:40 to Jul-31 02:40">
<filter>tf:CustomTimeframe?1533008450802:1533019250802</filter>
</filters>
</source>
<reportheader>
<reportdetails>
<user>test1</user>
</reportdetails>
</reportheader>
<data>
<chartdashlet name="jvm_mem_percent" description="" showabsolutevalues="false">
<measures structuretype="tree">
<measure measure="Memory Utilization - Memory Utilization (split by Agent)" color="#800080" aggregation="Maximum" unit="%" thresholds="false" drawingorder="1">
<measure measure="Memory Utilization - test@server1" color="#7aebd0" aggregation="Maximum" unit="%" thresholds="false">
<measurement timestamp="1533008460000" avg="11.116939544677734" min="11.007165908813477" max="11.143875122070312" sum="66.7016372680664" count="6"></measurement>
<measurement timestamp="1533008520000" avg="11.204706827799479" min="11.144883155822754" max="11.268420219421387" sum="67.22824096679688" count="6"></measurement>
</measure>
<measure measure="Memory Utilization - test@server2" color="#a6f2e0" aggregation="Maximum" unit="%" thresholds="false">
<measurement timestamp="1533008460000" avg="11.900418599446615" min="10.386141777038574" max="13.744248390197754" sum="71.40251159667969" count="6"></measurement>
<measurement timestamp="1533008520000" avg="11.139397939046225" min="10.617960929870605" max="11.427289009094238" sum="66.83638763427734" count="6"></measurement>
</measure>
<measure measure="Memory Utilization - test@server3" color="#dd2271" aggregation="Maximum" unit="%" thresholds="false">
<measurement timestamp="1533008460000" avg="8.395787556966146" min="8.340044021606445" max="8.429450035095215" sum="50.374725341796875" count="6"></measurement>
<measurement timestamp="1533008520000" avg="8.490419387817383" min="8.456218719482422" max="8.5205659866333" sum="50.9425163269043" count="6"></measurement>
</measure>
</measure>
</measures>
</chartdashlet>
<chartdashlet name="jvm_trans_errors" description="" showabsolutevalues="false">
<measures structuretype="tree"></measures>
</chartdashlet>
<chartdashlet name="jvm_trans" description="" showabsolutevalues="false">
<measures structuretype="tree">
<measure measure="Count Backend - Count Backend (split by Agent)" color="#8080c0" aggregation="Sum" unit="num" thresholds="false" drawingorder="1">
<measure measure="Count Backend - test@server1" color="#e44e8d" aggregation="Sum" unit="num" thresholds="false">
<measurement timestamp="1533010380000" avg="1.0" min="1.0" max="1.0" sum="1.0" count="1"></measurement>
<measurement timestamp="1533011340000" avg="1.0" min="1.0" max="1.0" sum="10.0" count="10"></measurement>
<measurement timestamp="1533013080000" avg="1.0" min="1.0" max="1.0" sum="1.0" count="1"></measurement>
<measurement timestamp="1533013200000" avg="1.0" min="1.0" max="1.0" sum="1.0" count="1"></measurement>
<measurement timestamp="1533014940000" avg="1.0" min="1.0" max="1.0" sum="2.0" count="2"></measurement>
<measurement timestamp="1533015780000" avg="1.0" min="1.0" max="1.0" sum="1.0" count="1"></measurement>
<measurement timestamp="1533018480000" avg="1.0" min="1.0" max="1.0" sum="1.0" count="1"></measurement>
<measurement timestamp="1533018540000" avg="1.0" min="1.0" max="1.0" sum="2.0" count="2"></measurement>
</measure>
<measure measure="Count Backend - test@server2" color="#e5cf4d" aggregation="Sum" unit="num" thresholds="false">
<measurement timestamp="1533009060000" avg="1.0" min="1.0" max="1.0" sum="10.0" count="10"></measurement>
<measurement timestamp="1533009120000" avg="1.0" min="1.0" max="1.0" sum="1.0" count="1"></measurement>
<measurement timestamp="1533009420000" avg="1.0" min="1.0" max="1.0" sum="3.0" count="3"></measurement>
<measurement timestamp="1533009480000" avg="1.0" min="1.0" max="1.0" sum="5.0" count="5"></measurement>
<measurement timestamp="1533010020000" avg="1.0" min="1.0" max="1.0" sum="4.0" count="4"></measurement>
<measurement timestamp="1533010320000" avg="1.0" min="1.0" max="1.0" sum="1200.0" count="1200"></measurement>
</measure>
<measure measure="Count Backend - test@server3" color="#dec321" aggregation="Sum" unit="num" thresholds="false">
<measurement timestamp="1533008460000" avg="1.0" min="1.0" max="1.0" sum="4.0" count="4"></measurement>
<measurement timestamp="1533008520000" avg="1.0" min="1.0" max="1.0" sum="5.0" count="5"></measurement>
<measurement timestamp="1533008580000" avg="1.0" min="1.0" max="1.0" sum="9.0" count="9"></measurement>
<measurement timestamp="1533008640000" avg="1.0" min="1.0" max="1.0" sum="5.0" count="5"></measurement>
</measure>
</measure>
</measures>
</chartdashlet>
</data>
</dashboardreport>

输出需要如下所示:

timestamp    max           count    node
1.53301E+12 11.14387512 6 Memory Utilization - test@server1
1.53301E+12 11.26842022 6 Memory Utilization - test@server1
1.53301E+12 13.74424839 6 Memory Utilization - test@server2
1.53301E+12 11.42728901 6 Memory Utilization - test@server2
1.53301E+12 8.429450035 6 Memory Utilization - test@server3
1.53301E+12 8.520565987 6 Memory Utilization - test@server3
1.53301E+12 1 1 Count Backend - test@server1
1.53301E+12 1 10 Count Backend - test@server1
1.53301E+12 1 1 Count Backend - test@server1
1.53301E+12 1 1 Count Backend - test@server1

我可以在 R 中这样做:

doc <- read_xml("C:/test1/test.xml")
dat<-xml_find_all(doc, ".//measure/measure") %>%
map_df(function(x) {
xml_find_all(x, ".//measurement") %>%
map_df(~as.list(xml_attrs(.))) %>%
select(-min, -avg, -sum) %>%
mutate(node=xml_attr(x, "measure"))
})

我需要在 python 中执行此操作,有什么想法吗?

最佳答案

一种方法是预处理您的 XML 文件,然后将其提供给 pandas。我在这个例子中使用了 ElementTree

例如:

import pandas as pd
import xml.etree.ElementTree as ET

def getMetrics(file_name):
tree = ET.parse(file_name)
root = tree.getroot()
result = []
for measure in root.iter('measure'): #Get all 'measure' tag
node = measure.attrib["measure"].split("-")[0].strip() #Get Node
for measurement in measure: #Get Metrics Information
if "timestamp" in measurement.attrib:
result.append(dict(node=node, timestamp=measurement.attrib.get("timestamp"), max=measurement.attrib["max"], count=measurement.attrib["count"]))
return result

df = pd.DataFrame(getMetrics(filename), columns=["timestamp", "max", "count", "node"]) #Form Dataframe
print(df)

df.to_csv("Your_Output.csv") #Write to CSV.

输出:

        timestamp                 max count                node
0 1533008460000 11.143875122070312 6 Memory Utilization
1 1533008520000 11.268420219421387 6 Memory Utilization
2 1533008460000 13.744248390197754 6 Memory Utilization
3 1533008520000 11.427289009094238 6 Memory Utilization
4 1533008460000 8.429450035095215 6 Memory Utilization
5 1533008520000 8.5205659866333 6 Memory Utilization
6 1533010380000 1.0 1 Count Backend
7 1533011340000 1.0 10 Count Backend
8 1533013080000 1.0 1 Count Backend
9 1533013200000 1.0 1 Count Backend
10 1533014940000 1.0 2 Count Backend
11 1533015780000 1.0 1 Count Backend
12 1533018480000 1.0 1 Count Backend
13 1533018540000 1.0 2 Count Backend
14 1533009060000 1.0 10 Count Backend
15 1533009120000 1.0 1 Count Backend
16 1533009420000 1.0 3 Count Backend
17 1533009480000 1.0 5 Count Backend
18 1533010020000 1.0 4 Count Backend
19 1533010320000 1.0 1200 Count Backend
20 1533008460000 1.0 4 Count Backend
21 1533008520000 1.0 5 Count Backend
22 1533008580000 1.0 9 Count Backend
23 1533008640000 1.0 5 Count Backend

根据评论进行编辑。如果您想从请求中传递 xml,请使用 ET.fromstring 并传递 r.contentr.text

例如:

import pandas as pd
import xml.etree.ElementTree as ET

def getMetrics(file_name):
root = ET.fromstring(file_name)
result = []
for measure in root.iter('measure'): #Get all 'measure' tag
node = measure.attrib["measure"].split("-")[0].strip() #Get Node
for measurement in measure: #Get Metrics Information
if "timestamp" in measurement.attrib:
result.append(dict(node=node, timestamp=measurement.attrib.get("timestamp"), max=measurement.attrib["max"], count=measurement.attrib["count"]))
return result

df = pd.DataFrame(getMetrics(r.content), columns=["timestamp", "max", "count", "node"]) #Form Dataframe
print(df)

关于python - 你如何在python中将xml文件转换为数据框或csv输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51750726/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com