我正在尝试分析两个大小约为6GB的文件。我需要同时分析它们,因为我需要同时查看两个文件中的一行(一个来自每个文件)。我尝试过类似这样的操作:
with open(fileOne, "r") as First_file:
for index, line in enumerate(First_file):
# Do some stuff here
with open(fileTwo, "r") as Second_file:
for index, line in enumerate(Second_file):
# Do stuff here aswell
问题在于第二个“with open”循环从文件开头开始。因此,分析所需的时间将会非常长。我还尝试了以下方法:
with open(fileOne, "r") as f1, open(fileTwo, "r") as f2:
for index, (line_R1, line_R2) in enumerate(zip(f1, f2)):
问题在于两个文件都直接加载到内存中。我需要从每个文件中获取相同的行。正确的行是:
number_line%4 == 1
这将会给出第2、5、9、13等行。我需要从两个文件中获取这些行。
有没有更快速和更节省内存的方法来做到这一点?