Sam
Sam

Reputation: 25

How to modify an xml string with python in synapse notebook

I have a pandas dataframe with a single row. Now I need to

  1. Convert this dataframe into an xml string and store it to a variable
  2. Modify the xml string variable to add header tags.
  3. Load the modified xml string variable into azure data lake storage.

When I try to covert the dataframe to xml using 'to_xml()' function, I am not able to store this into a variable. Can anyone please help me with this?

I use azure synapse notebooks (python) to do this process.

Upvotes: 0

Views: 99

Answers (1)

Bhavani
Bhavani

Reputation: 5317

Use the code below to convert a data frame into an XML string with a sample data frame:

import pandas as pd

data = {'Name': ['Alice'],
        'Age': [25],
        'City': ['New York']}
df = pd.DataFrame(data)
print(df)

xml_string = df.to_xml(index=False)
print(xml_string)

This will convert the data frame to an XML string, as shown below:

enter image description here

To add header tags to the XML string, modify it using the following code:

header = '<root>'  
footer = '</root>'

modified_xml_string = header + xml_string + footer
print(modified_xml_string)

The modified XML string is displayed as shown below:

enter image description here

Upvotes: 0

Related Questions