Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to convert list data to xml using python

I have been trying to convert list data to xml file.

But getting below error : ValueError: Invalid tag name '0'

This is my header : 'Name,Job Description,Course'

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Code:

import pandas as pd

lst = [ 'Name,Job Description,Course' , 
   'Bob,Backend Developer,MCA',  
   'Raj,Business Analyst,BMS', 
   'Alice,FullStack Developer,CS' ]

df = pd.DataFrame(lst)
with open('output.xml', 'w') as myfile: 
  myfile.write(df.to_xml())

>Solution :

The df you created is improper. There are two scenarios.

  1. If you took name, job description, course as single header. You
    will fail at the point of saving df to xml.
  2. In order to save df as xml there is a format that need to be followed.

Below solution works. Hope this is what you are trying to achieve.

import pandas as pd

lst = [ ['Name','Job_Description','Course'] , 
['Bob','Backend Developer','MCA'],  
['Raj','Business Analyst','BMS'], 
['Alice','FullStack Developer','CS'] ]

df = pd.DataFrame(lst[1:], columns=[lst[0]])
print(df)
df.to_xml('./output.xml')
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading