python读取文件存到excel中

用xlwt模块执行代码报下面的错 

ValueError: column index (256) not an int in range(256)

 

xlwt 模块看源码说最大列只支持255列,所以超过这个值就报错了,改用xlsxwriter模块

 

import xlsxwriter
workbook = xlsxwriter.Workbook('chineseQA.xlsx')     #创建工作簿
worksheet = workbook.add_worksheet()            #创建工作表

title=['question','answer']
lie = 0#定义要插入的列,0是第一列
for i in title:
    worksheet.write(0,lie,i)
    lie+=1


hang = 1#定义要插入的行,1是第二行
with open('question',encoding='utf-8') as f:
    lie1 = 0#定义要插入的列
    for i in f.readlines():
        lis=i.strip()
        worksheet.write(hang, lie1, lis)
        hang+=1#行数加一
    lie1+=1#列数加一

hang=1
with open('answer',encoding='utf-8') as f:
    lie1 = 1
    for i in f.readlines():
        lis=i.strip()
        worksheet.write(hang, lie1, lis)
        hang+=1
    lie1+=1
workbook.close()

 参考下面这篇博客

http://www.mamicode.com/info-detail-2252921.html

posted @ 2018-09-13 11:50  Operater  阅读(3554)  评论(0编辑  收藏