Jump to content

Python Xlwt Help


staroflaw

Recommended Posts

Hello, I am new to Python and need some help with xlwt.

I have a txt file I am trying to convert to a spredsheet. (XML)

line01
line02
line03
line04
line05
line06
line07
line08
line09
line10

How can I read the txt file and every 5 lines start a new Row.

line01     line02     line03     line04     line05
line06     line07     line08     line09     line10
line11     line12     line13     line14     line15

This is what I have so far.

import xlwt

book = xlwt.Workbook(encoding="utf-8")
sheet1 = book.add_sheet("Sheet1")


fileIN = open('out2.txt', 'r')
line = fileIN.readline()

#--NEED HELP WITH THE REST--


fileIN.close()
book.save("python_spreadsheet.xls") 

Link to comment
Share on other sites

This seems to work. May not be the best way to do it... But I got it working.

import xlwt

book = xlwt.Workbook(encoding="utf-8")
sheet1 = book.add_sheet("Python Sheet 1")


f=open('hstats.txt','r')
data_list = f.readlines()
f.close()


i=0 
x=4

r=0
c=0


for line in data_list:

  sheet1.write(r, c, line)

  i=i+1
  c=c+1


  if i > x:
    r=r +1
    c=0 
    x=x +5 


book.save("python_spreadsheet.xls")

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...