Home > database >  Dynamically increment lists to Python function
Dynamically increment lists to Python function

Time:04-02

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

CodePudding user response:

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