Python学习笔记-Txt文件转Excel文件
2016-08-21 15:30
507 查看
Txt文件转Excel 2003文件(Excel 2003 一个工作表行数限制65536,列数限制256)
Txt文件转Excel 2007文件(Excel 2007 一个工作表行数限制1048576,列数限制16384)
# -*- coding:utf-8 -*- import os import sys import xlwt import datetime default_encoding = 'utf-8' if sys.getdefaultencoding() != default_encoding: print sys.getdefaultencoding() reload(sys) sys.setdefaultencoding(default_encoding) if __name__=='__main__': startTime = datetime.datetime.now() if len(sys.argv)!=2: sys.exit(1) path=os.path.join(os.getcwd(), sys.argv[1]) if not os.path.exists(path): print "ERROR: %s can not find" %path sys.exit(1) xlsxPath = os.path.join(os.path.dirname(path), os.path.splitext(os.path.basename(path))[0] + '.xls') workbook = xlwt.Workbook(encoding='utf-8') BUFSIZE = 1024 EXCEL_ROWS = 65535 EXCEL_COLS = 256 FIELD_SEPARATOR = ',' with open(path, 'r') as f: nrows, total_rows = 0, 0 lines = f.readlines(BUFSIZE) while lines: for line in lines: if (nrows % EXCEL_ROWS == 0) : wsheet = workbook.add_sheet('sheet' + str(total_rows), cell_overwrite_ok = True) nrows = 0 values = line.split(FIELD_SEPARATOR) cols_num = EXCEL_COLS if len(values) > EXCEL_COLS else len(values) for ncol in xrange(cols_num): wsheet.write(nrows, ncol, values[ncol]) nrows = nrows + 1 total_rows = total_rows + 1 lines = f.readlines(BUFSIZE) workbook.save(xlsxPath) endTime = datetime.datetime.now() print "spend time %s seconds" %((endTime - startTime).seconds)
Txt文件转Excel 2007文件(Excel 2007 一个工作表行数限制1048576,列数限制16384)
# -*- coding:utf-8 -*- import os import sys import datetime import xlsxwriter default_encoding = 'utf-8' if sys.getdefaultencoding() != default_encoding: print sys.getdefaultencoding() reload(sys) sys.setdefaultencoding(default_encoding) if __name__ == '__main__': startTime = datetime.datetime.now() if len(sys.argv)!=2: sys.exit(1) path=os.path.join(os.getcwd(), sys.argv[1]) if not os.path.exists(path): print "ERROR: %s can not find" % path sys.exit(1) xlsxPath = os.path.join(os.path.dirname(path), os.path.splitext(os.path.basename(path))[0] + '.xlsx') workbook = xlsxwriter.Workbook(xlsxPath) BUFSIZE = 1024 EXCEL_ROWS = 1040000 EXCEL_COLS = 16384 FIELD_SEPARATOR = ',' with open(path, 'r') as f: nrows, total_rows, sheet_num = 0, 0, 0 lines = f.readlines(BUFSIZE) while lines: for line in lines: if (total_rows % EXCEL_ROWS == 0) : worksheet = workbook.add_worksheet(name = 'sheet' + str(sheet_num)) nrows = 0 sheet_num = sheet_num + 1 values = line.split(FIELD_SEPARATOR) cols_num = EXCEL_COLS if len(values) > EXCEL_COLS else len(values) for ncol in xrange(cols_num): worksheet.write(nrows, ncol, values[ncol]) nrows = nrows + 1 total_rows = total_rows + 1 lines = f.readlines(BUFSIZE) workbook.close() endTime = datetime.datetime.now() print "spend time %s seconds" % ((endTime - startTime).seconds)
相关文章推荐
- python学习笔记(1)--遍历txt文件,正则匹配替换文字
- Python学习笔记 --- python将 excel 文件转化为 csv 文件 (07版本及以下 和 07版本及以上)
- Python学习笔记(五)-实数/复数矩阵输出到txt文件(有格式选择)
- Python学习笔记 --- pandas将excel转化为csv文件
- 黑马程序员之C#学习笔记:C#读取txt文件的方法
- Python学习笔记-XML大文件的XSD验证方法
- Python3.3 学习笔记6 - 文件
- Python学习笔记整理(七)Python中的元组和文件及其他
- python 学习笔记(11)XML文件
- Python学习笔记 === python脚本文件快速浏览说明
- Python学习笔记——文件对象和操作(2)
- Python学习笔记--文件读写
- Python学习笔记---模块;文件与文本处理
- OpenCV学习笔记 cv.Mat 与 .txt 文件数据的读写操作
- Python 学习笔记——文件对象和操作
- Python学习笔记——文件对象和操作(1)
- python学习笔记——文件
- 学习笔记——通过EPPlus导出Excel文件
- Python学习笔记(三):文件和目录工具
- java学习笔记_将中文字符导出txt文档_从txt文档读取文件