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. however, it seems that the output from this code is that it only writes to one cell in the excel. Pls advise how to modify the code. Pls advise if there are alternative way for python to do this task? tq

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 
    #L = line.strip()
    L = list(line)
    sheet.write(row,0,L)    
    row += 1
  
wbk.save('reformatted.data.xls')

1. The logic of writting only to row will not help you.
you need to provide to collumn info too.

import xlwt

# Create workbook and worksheet 
wbk = xlwt.Workbook() 
sheet = wbk.add_sheet('python')

row = 0  # row counter
col=0  # col counter
f = open('robert.txt')

for line in f: 
    # separate fields by commas 
    #L = line.strip()
    L = list(line)
    sheet.write(row,col,L) ## basic logic    
    row += 1
    col +=1 ## you need this i think
wbk.save('reformatted.data.xls')

You must be ok i think ;)

and try csv files for excell . They just work!

hi richieking,

i didn't add a counter for column because i want the extracted data from the text file to be arranged in 1 column with separate rows.
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

tq

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.