enter image description here`My code converts text to xml via python, but there is a bug in my program. Not able to debug it. It only reads the 1st row, but as soon as the counter reaches 2nd line, the error indexerror:out of range occurs. Please help. If you see the file, each entry has 14 rows to be precise and these 14 entries are seperated by a pipe ( | ) delimiter. The code works fine if I keep adding entries in first row, but the moment i use 2nd row, it errors out.
The O/P of the program when all alphabets are in 1st row is attached in the image. import sysenter image description here The moment i shift any letter in 2nd role, my code is giving out of range error. No matter I enter row[0], row1, row[2] etc or insert row[i] and increment i each time, the stacktrace is the same
Traceback (most recent call last): File "c:\Python\Projects\xmlfile.py", line 17, in clnt.text = row1 IndexError: list index out of range
The contents of the text file are (text.txt): A|B|C|D|E|F|G|H|I|J|K|L|M|N| O
import csv
from lxml import etree as et
csv.field_size_limit(sys.maxsize)
root = et.Element("Processes")
i = 0
data = []
with open("test.txt") as file:
for row in csv.reader(file, delimiter="|"):
name = et.SubElement(root, "name")
time = et.SubElement(name, "Time")
time.text = row[i]
i = 1
clnt = et.SubElement(name, "Client")
clnt.text = row[i]
i = 1
usr = et.SubElement(name, "User")
usr.text = row[i]
i = 1
txtstat = et.SubElement(name, "textstatus")
txtstat.text = row[i]
i = 1
num = et.SubElement(name, "number")
num.text = row[i]
i = 1
pid = et.SubElement(name, "processid")
pid.text = row[i]
i = 1
prog = et.SubElement(name, "program")
prog.text = row[i]
i = 1
randnum = et.SubElement(name, "randomnumber")
randnum.text = row[i]
i = 1
pidandwp = et.SubElement(name, "processidandwp")
pidandwp.text = row[i]
i = 1
userclnt = et.SubElement(name, "userclient")
userclnt.text = row[i]
i = 1
tid = et.SubElement(name, "transactionid")
tid.text = row[i]
i = 1
add1 = et.SubElement(name, "additional1")
add1.text = row[i]
i = 1
add2 = et.SubElement(name, "additional2")
add2.text = row[i]
i = 1
add3 = et.SubElement(name, "additional3")
add3.text = row[i]
i = 1
add3 = et.SubElement(name, "additional3")
add3.text = row[i]
i = 1
add3 = et.SubElement(name, "additional3")
add3.text = row[i]
i = 1
add3 = et.SubElement(name, "additional3")
add3.text = row[i]
data.append(row)
xml_datas = et.tostring(root, pretty_print=True, xml_declaration=True, encoding="utf-8")
print(xml_datas.decode())`
CodePudding user response:
This should work better, It reads from the file and doesn't use i
in the way you were using it and can dynamically work with rows of any length.
import csv
from lxml import etree as et
csv.field_size_limit(sys.maxsize)
root = et.Element("Processes")
row_names = [
'Time',
'Client',
'User',
'number',
'processid',
'program',
'randomnumber',
'processidandwp',
'userclient',
'transactionid',
'additional1',
'additional2',
'additional3'
]
with open("test.txt") as file:
for row in csv.reader(file, delimiter="|"):
name = et.SubElement(root, "name")
for i in range(len(row)):
node = et.SubElement(name, row_names[i])
node.text = row[i]
xml_datas = et.tostring(root, pretty_print=True, xml_declaration=True, encoding="utf-8")
print(xml_datas.decode())`