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:
JavaScript
x
11
11
1
import pandas as pd
2
3
lst = [ 'Name,Job Description,Course' ,
4
'Bob,Backend Developer,MCA',
5
'Raj,Business Analyst,BMS',
6
'Alice,FullStack Developer,CS' ]
7
8
df = pd.DataFrame(lst)
9
with open('output.xml', 'w') as myfile:
10
myfile.write(df.to_xml())
11
Advertisement
Answer
The df you created is improper. There are two scenarios.
- If you took
name, job description, course
as single header. You will fail at the point of saving df to xml. - 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.
JavaScript
1
11
11
1
import pandas as pd
2
3
lst = [ ['Name','Job_Description','Course'] ,
4
['Bob','Backend Developer','MCA'],
5
['Raj','Business Analyst','BMS'],
6
['Alice','FullStack Developer','CS'] ]
7
8
df = pd.DataFrame(lst[1:], columns=[lst[0]])
9
print(df)
10
df.to_xml('./output.xml')
11