staroflaw Posted February 7, 2011 Share Posted February 7, 2011 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") Quote Link to comment Share on other sites More sharing options...
staroflaw Posted February 13, 2011 Author Share Posted February 13, 2011 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") Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.