【问题标题】:Reading simultaneously different number of lines from two files从两个文件中同时读取不同数量的行
【发布时间】:2017-03-21 16:24:27
【问题描述】:

我正在寻找一种方法来同时读取两个大文件,而无需将整个数据放入内存。我想用第二个文件中的 N 行来解析第一个文件中的 M 行。是否有任何明智且有效的内存解决方案?

到目前为止,我知道如何逐行同时读取两个文件。但我不知道是否可以扩展此代码以从第一个文件中读取例如 4 行,从第二个文件中读取 1 行。

from itertools import izip
with open("textfile1") as textfile1, open("textfile2") as textfile2: 
for x, y in izip(textfile1, textfile2):
    x = x.strip()
    y = y.strip()
    print("{0}\t{1}".format(x, y))

从这里,Read two textfile line by line simultaneously -python

【问题讨论】:

    标签: python file parallel-processing


    【解决方案1】:

    只需打开文件并使用例如line = textfile1.readline() 从其中一个文件中读取一行。

    line 将包含一个尾随换行符。当返回一个空字符串时,您会看到您到达了结尾。

    【讨论】:

      【解决方案2】:

      这将在其他代码中从 file1 读取接下来的 n 行,然后从 file2 读取接下来的 m 行

      def nextlines(number, file):
          n_items = []
          index = number
          while(index > 0):
              try:
                  n_items += [next(file).strip()]
                  index -= 1
              except StopIteration:
                  break
          return n_items
      
      n = 5
      m = 7
      l1 = []
      l2 = []
      with open('file.dat', 'r') as file1:
          with open('file.dat', 'r') as file2:
              #some code
              l1 = nextlines(n, file1)
              l2 = nextlines(m, file2)
              #some other code
              file2.close()
          file1.close()
      print l1
      print l2
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2012-12-31
        • 1970-01-01
        • 2018-06-06
        • 1970-01-01
        • 2012-07-02
        • 1970-01-01
        • 2020-06-30
        • 1970-01-01
        相关资源
        最近更新 更多