Hey,I wanted to know how to read a .txt file and the output should be in .xml format.
I have the input file as input1.txt
My code:
from lxml import etree
import csv
root = etree.Element('data')
#f = open('input1.txt','rb')
rdr = csv.reader(open("input1.txt",newline='\n'))
header = next(rdr)
for row in rdr:
eg = etree.SubElement(root, 'eg')
for h, v in zip(header, row):
etree.SubElement(eg, h).text = v
f = open(r"C:\temp\input1.xml", "w")
f.write(etree.tostring(root))
f.close()
I'm getting an error like:
Traceback (most recent call last):
File "E:\python3.2\input1.py", line 11, in <module>
etree.SubElement(eg, h).text = v
File "lxml.etree.pyx", line 2995, in lxml.etree.SubElement (src\lxml\lxml.etree.c:69677)
File "apihelpers.pxi", line 188, in lxml.etree._makeSubElement (src\lxml\lxml.etree.c:15691)
File "apihelpers.pxi", line 1571, in lxml.etree._tagValidOrRaise (src\lxml\lxml.etree.c:29249)
ValueError: Invalid tag name ' Paper 1'
And I want it to consider the white spaces also. I'm using Python 3.2. Any suggestions?