我想编写代码从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中,但是,它似乎不起作用。