I have the following txt file that has 4 fields that are tab separated: the first is the id and the other three show the results per test.

152 TEST1 valid TEST3 good TEST2 bad
158 TEST2 bad TEST1 bad TEST4 valid
.
.
.

Based on the above txt I need to create an xls file having as headers ID, TEST1, TEST2, TEST3, TEST4 and the values valid, bad, etc under the corresponding column:

ID TEST1 TEST2 TEST3 TEST4
152 valid bad good
158 bad bad valid

I tried to work that out with xlwt but couldn't. Actually, I started used the following code but I couldn't even split the line for further processing:

import xlwt
wbk = xlwt.Workbook()
sheet = wbk.add_sheet('sheet 1')

row = 0
f = open('C:/test.txt','r')
for line in f:
# separate fields by tab
L = line.rstrip().split('\t')

#actually some code is needed here to create the columns and put the values under the appropriate column
.....

# Write the data, using the style defined above.
sheet.write(row,0,ID)
sheet.write(row,1,TEST1)
sheet.write(row,2,TEST2)
sheet.write(row,3,TEST3)
sheet.write(row,4,TEST4)
row += 1

wbk.save('data.xls')


Thanks in advance for any help!

How about while or for loop?

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.