[Tutor] create an xls file using data from a txt file

Dave Angel davea at ieee.org
Wed May 11 10:38:40 CEST 2011


On 01/-10/-28163 02:59 PM, tax botsis wrote:
> 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 working
> on the following script 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')

Looks to me like you've split a line just fine.  This line needs to be 
indented, however, so that it's inside the for loop.

What happens if you simply add a
      print L

and temporarily comment out the rest of the code?

Once you're confident about what's in L, how about if you figure out 
what data you could put into sheet.write() instead of printing it ?
Hint:  you probably want a for loop to process the list L.

DaveA


More information about the Tutor mailing list