0

I have df that I turn into a python dictionary, then convert it to an xml format.

Column A Column B
getTitle status
getTitle timestamp
submitBook title
submitBook date
submitBook numPages

I used:

`book_df = df.groupby(['Column A'])\
                                .agg(lambda x: list(x)).to_dict()`

This yields:

{'getTitle': ['status','timestamp'],
'submitBook': ['title', 'date', numPages]}

Thank you.

My attempts to use recursion to turn it into an xml:

def create_tree(root, dict):

     for k, v in dict_tree.items():
            create_xml_tree(ET.SubElement(root, k), v)
        return root
   

YIELDS THIS RESULT:

<?xml version="1.0" ?>
<Books>
<getTitle>['status','timestamp']</getTitle>
<submitBook>['title', 'date', numPages]</submitBook>
</Books>

But I want the below result where all the keys are a child node and its children are the list of values.

<?xml version="1.0" ?>
<Books>

<getTitle>
<status></status>
<timestamp></timestamp>
</getTitle>

<submitBook>
<title></title>
<date></date>
<numPages></numPages>
</submitBook>

</Books>
Lilly
  • 1

1 Answers1

1

To achieve the desired XML output, you can modify your create_tree function as follows:


import xml.etree.ElementTree as ET

def create_tree(root, dictionary):
   for k, v in dictionary.items():
       node = ET.SubElement(root, k)
       if isinstance(v, dict):
           create_tree(node, v)
       else:
           for item in v:
               ET.SubElement(node, item)
   return root

Then you can call this function to create the XML tree as follows:


book_dict = {'getTitle': ['status', 'timestamp'], 'submitBook': ['title', 'date', 'numPages']}
root = ET.Element('Books')
create_tree(root, book_dict)
xml_string = ET.tostring(root, encoding='unicode', method='xml')
print(xml_string)

This should produce the following XML output:


<Books>
  <getTitle>
    <status />
    <timestamp />
  </getTitle>
  <submitBook>
    <title />
    <date />
    <numPages />
  </submitBook>
</Books>