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')
Attachments
01010100011111011111101111111011101100110011001100111100000000001100110001000111111000000000001100000000000000000111000000000000101101111101101111000000000011000001000110001000110001000110001000110001000110001000110000010100100110010110011001010101010101010101010100000000000000000011111000110000100001000000000001110111111101000100000000000000011100111111111110111111111111111111111010111111111111111111101101111111111100011101001110011111111010000000100000000000000000000000000000000000000000000000000111110111111111111011101111101111101111111111100111100001110111111111

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!

Edited 5 Years Ago by richieking: n/a

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

This question has already been answered. Start a new discussion instead.