我想编写代码以从 Excel 获取数据并将其写入文本文件。这是我的代码:
import xlrd
import os.path
wb = xlrd.open_workbook(os.path.join('D:\TRB 2014 Data','SPS1 demo data.xlsx'))
wb.sheet_names()
sh = wb.sheet_by_index(0)
i = 1
while sh.cell(i,11).value != 0:
Load = sh.cell(i,11).value
D1 = sh.cell(i,13).value
D2 = sh.cell(i,14).value
D3 = sh.cell(i,15).value
D4 = sh.cell(i,16).value
D5 = sh.cell(i,17).value
D6 = sh.cell(i,18).value
D7 = sh.cell(i,19).value
DB1 = str(Load) + " " + str(D1) + " " + str(D2) + " " + str(D3)+ " " + str(D4)+ " " + str(D5)+ " " + str(D6)+ " " + str(D7)
file = open("Output.txt", "w")
file.write(DB1 + '\n')
file.close
i = i + 1
这段代码的问题是写入文本文件的数据总是显示在第一行。因此,虽然我在 excel 中有 20 行数据,但文本文件仅在文本文件的第一行显示 excel 文件中的最后一个数据。我有'\n'
,file.write
但是,它似乎不起作用。