Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

How to convert an XML file to nice pandas dataframe

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

def iter_docs(author):
    author_attr = author.attrib
    for doc in author.iter('document'):
        doc_dict = author_attr.copy()
        doc_dict.update(doc.attrib)
        doc_dict['data'] = doc.text
        yield doc_dict

xml_data = io.StringIO(u'''YOUR XML STRING HERE''')

etree = ET.parse(xml_data) #create an ElementTree object 
doc_df = pd.DataFrame(list(iter_docs(etree.getroot())))
Comment

PREVIOUS NEXT
Code Example
Python :: pygame borders on window 
Python :: python raw strings 
Python :: list[:] 
Python :: Compress multiple directories but exclude directory - Python zipfile(or anything native to Windows 2012+ 
Python :: print banner in python 
Python :: preallocate numpy array 
Python :: Examples of correct code for this rule with global declaration: 
Python :: matrix implement 
Python :: decoding to str: need a bytes-like object, list found 
Python :: how do i access individual elements of matrix in python? 
Python :: string exercise 
Python :: How to correctly call url_for and specify path parameters 
Python :: python logical operators code in grepper 
Python :: how to wait 5 seconds in python 
Python :: shere point file uploading to doc repository python 
Python :: clock replacement algorithm python 
Python :: ring PostgreSQL load the postgresqllib.ring library 
Python :: Select right color to threshold and image with opencv 
Python :: ring Desktop, WebAssembly and Mobile Using QTreeView and QFileSystemModel 
Python :: void setup and void loop 
Python :: weigted average in pandas 
Python :: how to use random ranint 
Python :: biodiversity 
Python :: Print Wavelet modes 
Python :: removeStopWords 
Python :: how to change text in a canvas tkinter 
Python :: remove stopwords python 
Python :: python remainder divide by 60 
Python :: how to iterate a dictionary with minimum value in python 
Python :: How to call any function with it name as a string 
ADD CONTENT
Topic
Content
Source link
Name
4+1 =