有一个名为:file.txt的文本文档,上面已有文字。
当我浏览excel表并写入文本文件时,它会删除原始文本。
如何在保留原始文字信息的同时附加Excel工作表中的信息?
CODE
import xlwt
import xlrd
import csv
workbook = xlrd.open_workbook('input.xls')
sheet = workbook.sheet_by_index(2)
data = []
data.append([sheet.cell_value(row, 0).strip() for row in range(sheet.nrows)])
data.append([sheet.cell_value(row, 1).strip() for row in range(sheet.nrows)])
workbook = xlwt.Workbook()
sheet = workbook.add_sheet('test')
for colidx, col in enumerate(data):
for rowidx, row in enumerate(col):
sheet.write(rowidx, colidx, row)
transposed = zip(*data)
with open('file.txt','wb') as fou:
writer = csv.writer(fou)
for row in transposed:
writer.writerow(row)
答案 0 :(得分:1)
如果我理解正确,您希望将ab
作为标记传递给open()
:
transposed = zip(*data)
# v
with open('file.txt','ab') as fou:
writer = csv.writer(fou)
for row in transposed:
writer.writerow(row)