XLWT转换为excel文件

在我的应用程序中,我试图用xlwt将数据写入excel文件。

源代码:

# -*- 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() 

名为OK.txt的文件包含:

 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 

有20列。 当我删除一列或删除一个值时,什么也没有。 白纸。

该行: –

 if len(tokens) != 21: continue 

如果有21列,则跳过循环。 看看是否有帮助。