内存错误 Python 逐行处理大文件

Memory Error Python Processing Large File Line by Line

我正在尝试连接模型输出文件,由于软件输出到文件的方式,模型 运行 被分成 5 个,每个输出对应于其中一个部分 运行它从每个文件输出的 0 开始重新标记。我写了一些代码到:

1) 将所有输出文件连接在一起 2) 编辑合并文件以重新标记所有时间步长,从 0 开始并在每个时间步长增加。

目的是我可以将这个单个文件一次性加载到我的可视化软件中,而不是打开 5 个不同的 windows。

到目前为止,由于我正在处理的大文件,我的代码抛出了一个内存错误。

我有一些关于如何尝试摆脱它的想法,但我不确定什么会起作用 or/and 可能会使事情变得缓慢。

到目前为止的代码:

import os
import time

start_time = time.time()

#create new txt file in smae folder as python script

open("domain.txt","w").close()


"""create concatenated document of all tecplot output files"""
#look into file number 1

for folder in range(1,6,1): 
    folder = str(folder)
    for name in os.listdir(folder):
        if "domain" in name:
            with open(folder+'/'+name) as file_content_list:
                start = ""
                for line in file_content_list:
                    start = start + line# + '\n' 
                with open('domain.txt','a') as f:
                    f.write(start)
              #  print start

#identify file with "domain" in name
#extract contents
#append to the end of the new document with "domain" in folder level above
#once completed, add 1 to the file number previously searched and do again
#keep going until no more files with a higher number exist

""" replace the old timesteps with new timesteps """
#open folder named domain.txt
#Look for lines:
##ZONE T="0.000000000000e+00s", N=87715, E=173528, F=FEPOINT, ET=QUADRILATERAL
##STRANDID=1, SOLUTIONTIME=0.000000000000e+00
# if they are found edits them, otherwise copy the line without alteration

with open("domain.txt", "r") as combined_output:
    start = ""
    start_timestep = 0
    time_increment = 3.154e10
    for line in combined_output:
        if "ZONE" in line:
            start = start + 'ZONE T="' + str(start_timestep) + 's", N=87715, E=173528, F=FEPOINT, ET=QUADRILATERAL' + '\n'
        elif "STRANDID" in line:
            start = start + 'STRANDID=1, SOLUTIONTIME=' + str(start_timestep) + '\n'
            start_timestep = start_timestep + time_increment
        else:
            start = start + line

    with open('domain_final.txt','w') as f:
        f.write(start)

end_time = time.time()
print 'runtime : ', end_time-start_time

os.remove("domain.txt")

到目前为止,我在连接阶段得到了内存错误。

要改进我可以:

1) 在我阅读每个文件时尝试在旅途中进行更正,但由于它已经无法通过整个文件,我认为除了计算时间之外不会有太大的不同

2) 将所有文件加载到一个数组中,并使检查和运行函数在数组上起作用:

类似于:

def do_correction(line):
        if "ZONE" in line:
            return 'ZONE T="' + str(start_timestep) + 's", N=87715, E=173528, F=FEPOINT, ET=QUADRILATERAL' + '\n'
        elif "STRANDID" in line:
            return 'STRANDID=1, SOLUTIONTIME=' + str(start_timestep) + '\n'
        else:
            return line

3) 保持原样并要求 Python 指示何时 运行 内存不足并在该阶段写入文件。有人知道这是否可能吗?

感谢您的帮助

在写入输出文件之前,不必将每个文件的全部内容读入内存。大文件只会消耗所有可用内存。

只需一次读写一行。也只打开一次输出文件...并选择一个不会被拾取并被视为输入文件本身的名称,否则你 运行 将输出文件连接到自身的风险(这还不是问题,但如果您还处理当前目录中的文件,则可能是这样)-如果加载它还没有消耗所有内存。

import os.path

with open('output.txt', 'w') as outfile:
    for folder in range(1,6,1): 
        for name in os.listdir(folder):
            if "domain" in name:
                with open(os.path.join(str(folder), name)) as file_content_list:
                    for line in file_content_list:
                        # perform corrections/modifications to line here
                        outfile.write(line)

现在您可以以面向行的方式处理数据 - 只需在写入输出文件之前对其进行修改。