tee chwee liong wrote:
hi,
i found a module called xlwt (http://www.python-excel.org/) that can write to Excel. i want the code to read from a file (robert.txt) and then write to excel in a column. for eg:
cell col0, row0 = 0
cell col0, row1 = 0
cell col0, row2 = 0
cell col0, row3 = 1
cell col0, row4 = 0
cell col0, row5 = 1
however, it seems that the output from this code is that it only writes to one
cell in the excel.
That's because your data ("robert.txt") only contains one line with one
field. You read that one field as a big chunk of text, and then write it
to one cell, and then the loop ends.
import xlwt
# Create workbook and worksheet
wbk = xlwt.Workbook()
sheet = wbk.add_sheet('python')
row = 0 # row counter
f = open('robert.txt')
for line in f:
# separate fields by commas
What does this comment mean? You don't have any commas in the file
"robert.txt", and you don't actually do anything with or to commas in
your code. I think that comment is false.
L = line.strip()
sheet.write(row,0,L)
Here you write the entire contents of the file into one cell. You need
to iterate over L:
for c in L:
sheet.write(row, 0, c)
row += 1
--
Steven
_______________________________________________
Tutor maillist - Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor