私は、複数のxml要素を持つ巨大なファイルを処理しようとする次のコードを持っています。Python - 大きなファイルを読む
from shutil import copyfile
files_with_companies_mentions=[]
# code that reads the file line by line
def read_the_file(file_to_read):
list_of_files_to_keep=[]
f = open('huge_file.nml','r')
lines=f.readlines()
print("2. I GET HERE ")
len_lines = len(lines)
for i in range(0,len(lines)):
j=i
if '<?xml version="1.0"' in lines[i]:
next_line = lines[i+1]
write_f = open('temp_files/myfile_'+str(i)+'.nml', 'w')
write_f.write(lines[i])
while '</doc>' not in next_line:
write_f.write(next_line)
j=j+1
next_line = lines[j]
write_f.write(next_line)
write_f.close()
list_of_files_to_keep.append(write_f.name)
return list_of_files_to_keep
このファイルのサイズは700 MBを超え、2,000万行以上あります。それを処理するより良い方法はありますか?
ご覧のとおり、前の行と次の行を参照変数(i
など)で参照する必要があります。
私が直面している問題は、非常に遅いということです。すべてのファイルに1時間以上かかり、複数のファイルがあります。
あなたが直面している問題は何ですか?ディスクスペース? – splinter
非常に遅いです。私は元の投稿を編集しました。 – adrCoder
これらのファイルのいくつかを同時に処理する並列処理はどうでしょうか? – splinter