Reputation:
In my application i'm trying to write the data to excel file using xlwt.
Source code:
# -*- coding: utf-8 -*-
import sys,os
from xlwt import Workbook, easyxf, Formula
#define the extension
print """Extension supported: xls, xlsx, ods"""
ext=raw_input("Extension: ")
def export2excel():
'''Edit the OK.txt file to add new lines following the upper lines
'''
fp = file('~/OK.txt')
lines = fp.readlines()
wb = Workbook()
ws = wb.add_sheet('Test')
ulstyle = easyxf('font: underline single')
r = 0
for line in lines:
tokens = line.strip().split('\t')
if len(tokens) != 21:
continue
for c,t in enumerate(tokens):
for dtype in (int,float):
try:
t = dtype(t)
except:
pass
else:
break
ws.write(r,c+0,t)
r += 1
a=wb.save("file." + ext)
print 'File exported in ' + os.getcwd()
if __name__ == "__main__":
export2excel()
The file called OK.txt contains:
1 Nahant 0 2011 -70.955000 42.255000 24.0 24.0 0.00632 18.0 2.31 0 0.538 6.575 65.2 4.0900 1 296 15.3 396.90 1
2 nahant 0 2011 -70.966000 42.599281 24.0 24.0 0.00634 17.0 2.12 0.1 12.2 6.571 61.2 3.0100 1 926 19.3 391.09 2
There are 20 columns. When i delete a column or i delete a value there is nothing. Blank sheet.
Upvotes: 1
Views: 437
Reputation: 2454
The line :-
if len(tokens) != 21:
continue
skips the loop if there are 21 columns. See if removing it helps.
Upvotes: 1