Eme*_*_sh 3 python performance for-loop bigdata
我需要找到包含特定字符串的文本文件的所有行,并将每行写入不同的文本文件中.如何改进我的代码以防止系统崩溃,以便读取大量文本文件(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)
你可以试试这段代码:
file_input = open('mhyfile.txt','r')
for line in file_input:
#Your code here
Run Code Online (Sandbox Code Playgroud)
该for line in file_input:循环将逐行读取文件中的行.但我在我的linux系统中测试并发现fileinput.input()不再使用内存.我想你应该提供更多关于你的问题的信息.
一个可能的问题是您将太多文件写入磁盘并导致系统崩溃.您可以尝试将所选行写入单个文件并标记行号j.