python 写入excel 问题一直报错
txt文件里面是12,2.12,15.3,45,12.12,54,12,...(20行)importxlwt3asxlwtwb=xlwt.Workbook()ws=wb....
txt文件里面是
12,2.12,15.3,
45,12.12,54,12,
...(20行)
import xlwt3 as xlwt
wb=xlwt.Workbook()
ws=wb.add_sheet('hongan3')
book=open('61610400.txt','r')
list2=[1,2]
ws.write(1,1,list2[1])
for s in book.readlines():
list=s.split(',')
print(list)
chang=len(list)
print(chang)
for i in range(1,chang):
print(list[i])
ws.write(1,1,list2[1]) #总是这里报错
wb.save('hongan11.xls')
book.close()
报错主要说是重写表格了 展开
12,2.12,15.3,
45,12.12,54,12,
...(20行)
import xlwt3 as xlwt
wb=xlwt.Workbook()
ws=wb.add_sheet('hongan3')
book=open('61610400.txt','r')
list2=[1,2]
ws.write(1,1,list2[1])
for s in book.readlines():
list=s.split(',')
print(list)
chang=len(list)
print(chang)
for i in range(1,chang):
print(list[i])
ws.write(1,1,list2[1]) #总是这里报错
wb.save('hongan11.xls')
book.close()
报错主要说是重写表格了 展开
1个回答
展开全部
def eachrowintext(filename, spliter=','):
with open(filename, 'r') as handle:
for ln in handle:
yield ln.strip().split(spliter)
def write2sheet(sheet, iter_rows, baserow=1, basecol=1):
for j, row in enumerate(iter_rows):
for i, item in enumerate(row):
sheet.write(baserow+j, basecol+i, item)
wb=xlwt.Workbook()
ws=wb.add_sheet('hongan3')
write2sheet(ws, eachrowintext('61610400.txt'))
wb.save("hongan11.xls")
with open(filename, 'r') as handle:
for ln in handle:
yield ln.strip().split(spliter)
def write2sheet(sheet, iter_rows, baserow=1, basecol=1):
for j, row in enumerate(iter_rows):
for i, item in enumerate(row):
sheet.write(baserow+j, basecol+i, item)
wb=xlwt.Workbook()
ws=wb.add_sheet('hongan3')
write2sheet(ws, eachrowintext('61610400.txt'))
wb.save("hongan11.xls")
追问
非常感谢你的回答。
我希望按照我的大致思路去写,主要我熟悉这样的形式一点。能帮忙改一下我的程序么,报错说overwrite
追答
关键在循环内的ws.write方法的参数1,1,..:每次均往固定的单元格Cell(A1)中写入,所以有这个问题。
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询