小编Eme*_*_sh的帖子

Python for循环导致系统崩溃

我需要找到包含特定字符串的文本文件的所有行,并将每行写入不同的文本文件中.如何改进我的代码以防止系统崩溃,以便读取大量文本文件(6GB大小)的第一个5,000,000,000行?编译代码后,我的电脑运行缓慢,并突然冻结.即使我停止编译过程,内存仍然被占用,同样的问题出现了.我的IDE是Spyder,我使用的是Python 2.7.谢谢!

我的代码是:

import fileinput

ot = 'N'
j = 1
i = 1
string = "ABCD"

for line in fileinput.input(['/../myfile.txt']):
    if i<=5000000000:
        if string in line:
            output = open(ot + str(j) + '.txt', 'w')
            output.write(line)
            output.close()
            j += 1
        i += 1
Run Code Online (Sandbox Code Playgroud)

python performance for-loop bigdata

3
推荐指数
1
解决办法
3237
查看次数

标签 统计

bigdata ×1

for-loop ×1

performance ×1

python ×1