Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

XML to table form in Excel

def has_children(e):
    ''' Check if element, e, has children'''
    return(len(list(e)) > 0)

def has_attrib(e):
    ''' Check if element, e, has attributes'''
    return(len(e.attrib)>0)

def get_uniqe_key(mydict, key):
    ''' Generate unique key if already exists in mydict'''
    if key in mydict:
        while key in mydict:
            key = key + '*'

    return(key)

tree = ET.parse('input2.xml')

root = tree.getroot()

# Get first level:
lvl_one = list(root)

myList = [];
for e in lvl_one:

    mydict  = {}
    # Iterate over each node in level one element
    for node in e.iter():

        if (not has_children(node)) & (node.text != None):
            uniqe_key = get_uniqe_key(mydict, node.tag)
            mydict[uniqe_key] = node.text

        if has_attrib(node):
            for key in node.attrib:
                uniqe_key = get_uniqe_key(mydict, key)
                mydict[uniqe_key] = node.attrib[key]

    myList.append(mydict)

print(pd.DataFrame(myList))
Comment

XML to table form in Excel

def has_children(e):
    ''' Check if element, e, has children'''
    return(len(list(e)) > 0)

def has_attrib(e):
    ''' Check if element, e, has attributes'''
    return(len(e.attrib)>0)

def get_uniqe_key(mydict, key):
    ''' Generate unique key if already exists in mydict'''
    if key in mydict:
        while key in mydict:
            key = key + '*'

    return(key)

tree = ET.parse('input2.xml')

root = tree.getroot()

# Get first level:
lvl_one = list(root)

myList = [];
for e in lvl_one:

    mydict  = {}
    # Iterate over each node in level one element
    for node in e.iter():

        if (not has_children(node)) & (node.text != None):
            uniqe_key = get_uniqe_key(mydict, node.tag)
            mydict[uniqe_key] = node.text

        if has_attrib(node):
            for key in node.attrib:
                uniqe_key = get_uniqe_key(mydict, key)
                mydict[uniqe_key] = node.attrib[key]

    myList.append(mydict)

print(pd.DataFrame(myList))
Comment

PREVIOUS NEXT
Code Example
Python :: pygame getting your charecter to jump 
Python :: list x[:-1] 
Python :: pandas dataframe select columns multiple cell value 
Python :: List change after copy Python 
Python :: map reduce and filter functions in python 
Python :: how to create function python 
Python :: extract data using selenium and disable javascript 
Python :: python flask many to many relation db 
Python :: pyqt serial plotter 
Python :: how do i add two matrix and store it in a list in python 
Python :: How to setup Conda environment and package access extension from within Jupyter 
Python :: how to seperate the script from html template when using jQuery in flask 
Python :: Creating 2-dimesional array 
Python :: cyclic rotation python 
Python :: python go back one using abspath 
Python :: random pick and remove index pandas 
Python :: ring write the same example using normal for loop the Encrypt() and Decrypt() functions. 
Python :: candelstick chart matplotlib 
Python :: ring Creating Reports using the WebLib and the GUILib 
Python :: salamelecus 
Python :: python sort array custom comparator 
Python :: downloading datasets from ml.org repository 
Python :: pip is not recognized as an internal or external command 
Python :: django how to create superuser if does not exists on migration 
Python :: django reverse accessor clashes for abstract class 
Python :: Pandas: Filter column value in array/list - ValueError: The truth value of a Series is ambiguous 
Python :: count numbers that add up to 10 in python 
Python :: what does alpha in plt.txt do 
Python :: python turn list of strings into list of doubles 
Python :: Select a Column in pandas data Frame Using Brackets 
ADD CONTENT
Topic
Content
Source link
Name
1+1 =