Home > Blockchain >  How to convert list data to xml using python
How to convert list data to xml using python

Time:06-05

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'

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())

CodePudding user response:

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')
  • Related