从 excel sheet 写入 txt 文档时追加数据

Appending data when writing from excel sheet to txt document

有一个名为:file.txt 的文本文档,上面已有文字。

当我浏览 excel sheet 并写入文本文件时,它会删除其中的原始文本。

如何在保留原始文本信息的同时仅附加 excel sheet 中的信息?

代码

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)

如果我理解正确,你想将 ab 作为标志传递给 open():

transposed = zip(*data)
#                     v
with open('file.txt','ab') as fou:
    writer = csv.writer(fou)
    for row in transposed:
        writer.writerow(row)