【问题标题】:How to read data from Excel and write it to text file line by line?如何从 Excel 中读取数据并逐行写入文本文件?
【发布时间】:2016-07-23 04:14:23
【问题描述】:

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

【问题讨论】:

    标签: python xlrd


    【解决方案1】:

    你应该open output.txt 文件和append mode:

    file = open("Output.txt", "a")
    

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


    更新

    在这种情况下,您可以使用with 而不是在最后关闭文件句柄。

    还包括@Josh in his own answer提出的好建议,代码可能是这样的:

    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
    

    【讨论】:

      【解决方案2】:
      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
      

      【讨论】:

      • 当工作表名称未知时最好使用sh = wb.sheet_by_index(0)
      【解决方案3】:

      您正在打开和关闭 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
      

      【讨论】:

      • 这也应该可以提高性能,因为文件不会一遍又一遍地打开和关闭。
      • 一个问题是保存的文本文件中没有任何内容
      • 您是否尝试将file.close() 作为最后一行?
      • 我做到了。文件创建和最终保存的文件之间可能存在延迟。我会仔细检查代码是否有效。
      • 按照 Nicolás 的回答中的建议使用“with”
      【解决方案4】:

      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
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2014-10-29
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多