pandas.Dataframe из xml

python > pandas.Dataframe из xml
24.10.2019 14:12:57


Наиболее часто встречающиеся слова в статье:

[element] [parse_element] [XML2DataFrame] [xml_data] [parse_root] [process_data] [structure_data] [ElementTree] [__init__] [parsed=None]


Статья:

import xml.etree.ElementTree as ET
import pandas as pd
 
class XML2DataFrame:
 
    def __init__(self, xml_data):
        self.root = ET.XML(xml_data)
 
    def parse_root(self, root):
        return [self.parse_element(child) for child in iter(root)]
 
    def parse_element(self, element, parsed=None):
        if parsed is None:
            parsed = dict()
        for key in element.keys():
            parsed[key] = element.attrib.get(key)
        if element.text:
            parsed[element.tag] = element.text
        for child in list(element):
            self.parse_element(child, parsed)
        return parsed
 
    def process_data(self):
        structure_data = self.parse_root(self.root)
        return pd.DataFrame(structure_data)
 
f = open('z.xml')
zz=f.read()
xml2df = XML2DataFrame(zz)
df = xml2df.process_data()