Dynamically increment lists to Python function

Viewed 64

I am trying to implement a function that receives a dynamically number of node names from *args and concatenates it to my xmltodict.parse(). Is it possible? Below is an example of what I'm trying to achieve where *args would pass 3 arguments (first, second and third node):

def parseXML(xml, *args):
    xd = xmltodict.parse(xml)['First_Node']['Second_Node']['Third_Node']
    df = pd.json_normalize(xd)
    
    return df
1 Answers

The following should work

def parseXML(xml, *args):
    xd = xmltodict.parse(xml)
    for arg in args:
        xd = xd[arg]
    df = pd.json_normalize(xd)  
    return df
Related