如何从Excel读取数据并逐行写入文本文件?

4

我想编写代码从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文件中的最后一个数据,而且显示在文本文件的第一行。我在file.write中使用了'\n',但似乎没有起作用。
4个回答

5
你应该使用追加模式打开output.txt文件:
file = open("Output.txt", "a")

在进入循环之前,您应该执行此操作,并且在该循环之后应将其关闭。

更新:

在类似这种情况下,你可以使用with代替在结尾处关闭文件句柄。

此外,包括@Josh在他自己的答案中提出的好建议, 代码可以是这样的:

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
with open("Output.txt", "a") as my_file:
    while sh.cell(i,11).value != 0:
        Load = sh.cell(i,11).value
        all_d = sh.col_values(i, 13, 19)
        DB1 = Load + " " + (" ".join(all_d))
        my_file.write(DB1 + '\n')
        i += 1

4
import xlrd

workbook=xlrd.open_workbook("xxx.xls")
sh=workbook.sheet_by_name("test1")
print sh.nrows
print sh.ncols
n=0
i=0
file=open("xxx.txt","w")
for n in range(sh.nrows):
    for i in range(sh.ncols):
        data =sh.cell_value(n,i)+" "
        print  data,
        file.write(data+" ")
    print 
    file.write("\n")
 this code is working properly for writing into text file

1
当工作表名称未知时,最好使用sh = wb.sheet_by_index(0) - Arun Kumar

2

你正在每行Excel表中打开和关闭输出文件。

在while循环之前打开文件,当循环完成后关闭文件。

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
file = open("Output.txt", "w")
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.write(DB1 + '\n')
   i = i + 1
file.close

这也应该提高性能,因为文件不会一遍又一遍地被打开和关闭。 - user2366842
其中一个问题是保存的文本文件中没有任何内容。 - user2639171
你试过在最后一行加上 file.close() 吗? - Nicolás Ozimica
我已经完成了。也许在文件创建和最终保存文件之间存在延迟。我会再次检查代码是否有效。 - user2639171
使用Nicolás的建议,使用“with” - Spaceghost

2

实际上,xlrd有一个很好的函数可以获取一列或一行中许多单元格的值。使用它,您可以大大简化代码(我相信他们的函数更有效率)。这样,您的代码可以变成:

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
my_file = open("Output.txt", "a")

while sh.cell(i,11).value != 0:
    Load = sh.cell(i,11).value
    all_d = sh.col_values(i, 13, 19)
    DB1 = Load+" "+(" ".join(all_d))
    my_file.write(DB1 + '\n')
    i += 1
file.close

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接