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
0376d295e822f9bd4db190cdd215e996
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?

Where did you get that code ? Your input file is not a csv file. You'll have to write your own parser for the text file. Perhaps the starting point could be to write your desired xml output by hand. You must first determine where you want to go.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.