如何在python

时间:2018-08-08 15:43:55

标签: python

我有这个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做到这一点,有什么想法吗?

4 个答案:

答案 0 :(得分:3)

一种方法是对XML文件进行预处理,然后将其提供给熊猫。在此示例中,我正在使用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)

答案 1 :(得分:0)

您应该在Python中使用内置库xml

现在,您的标签和属性不是标准的,因此我不得不创建一个可能为您的问题进行硬编码的函数,但其​​他人可以将其用作准则。

将这种标记视为您拥有的唯一数据源,并从父标记获取其node属性:

<measurement timestamp="1533008520000" avg="8.490419387817383" min="8.456218719482422" max="8.5205659866333" sum="50.9425163269043" count="6"></measurement>

以下功能应该起作用,使用Pandas创建一个数据框并将其导出到.csv文件:

from xml.dom import minidom
import pandas as pd

def convert():
    filename = 'teststack.xml'
    document = minidom.parse(filename)
    items = document.getElementsByTagName('measurement')

    df = pd.DataFrame(columns=["timestamp", "max", "count", "node"])
    for i, item in enumerate(items):
        # Creating new line for every item
        df.loc[i] = [
            item.getAttribute('timestamp'),
            item.getAttribute('max'),
            item.getAttribute('count'),
            item.parentNode.getAttribute('measure')
        ]

    # Exporting file
    df.to_csv("export.csv")
    return df

只需使用.xml文件更改文件名,它就可以工作。有了数据框后,就可以工作了,但是您想修改数据的精度,近似值和其他特征。

答案 2 :(得分:0)

这是仅使用随附库和Python 3.6的解决方案-无需熊猫

CSV:

import csv
import xml.etree.ElementTree

e = xml.etree.ElementTree.parse('data.xml').getroot()

with open('out.csv', 'w', newline='') as csv_file:
    csv_writer = csv.writer(csv_file)
    for data in e.iter('measures'):
        measures = data.findall('measure/measure')
        for measure in measures:
            for row in measure:
                csv_writer.writerow([row.get('timestamp'), row.get('max'), row.get('count'), measure.get('measure')])

列:

import xml.etree.ElementTree

e = xml.etree.ElementTree.parse('data.xml').getroot()

row_data = [['timestamp', 'max', 'count', 'node']]
widths = [len(i) for i in row_data[0]]

for data in e.iter('measures'):
    measures = data.findall('measure/measure')
    for measure in measures:
        for row in measure:
            row_list = [row.get('timestamp'), row.get('max'), row.get('count'), measure.get('measure')]
            row_data.append(row_list)

            for i, val in enumerate(row_list):
                if len(val) > widths[i]:
                    widths[i] = len(val)

with open('out.txt', 'w') as txt_writer:
    for row in row_data:
        txt_writer.write(' '.join([f"{row[i]: <{widths[i]}}" for i in range(4)]) + '\n')

答案 3 :(得分:0)

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.