0
import xlwt
wbk = xlwt.Workbook()
sheet = wbk.add_sheet('python')
row = 0
f = open('newfile.txt')
for line in f:
L = line.strip().split()
for i,c in enumerate(L):
    sheet.write(row,i,c)
    row += 1
    wbk.save('examp1.xls')

in this code it is inserted into columns but each column gets iterating not in the same row

1
  • What is your question? Can you please show example of what is in newfile.txt and what you are getting? and what you expected? Commented Aug 13, 2015 at 3:59

1 Answer 1

1

Remember that python is a language in which code blocks are defined by the indentation level. If you want to put each field from a CSV into a separate column in a spreadsheet, the second loop should be an inner loop of the first. Unless I am very much mistaken you don't want to save the file after every operation. That would cause a massive slow down.

import xlwt
wbk = xlwt.Workbook()
sheet = wbk.add_sheet('python')
row = 0
f = open('newfile.txt')
for line in f:
   L = line.strip().split()
   for i,c in enumerate(L):
       sheet.write(row,i,c)
       row += 1

wbk.save('examp1.xls')
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.