我的文件一有 240 万行 (256mb),文件二有 32000 行 (1.5mb)。
我需要逐行浏览文件二并打印文件一中的匹配行。
伪代码:
open file 1, read
open file 2, read
open results, write
for line2 in file 2:
for line1 in file 1:
if line2 in line1:
write line1 to results
stop inner loop
我的代码:
p = open("file1.txt", "r")
d = open("file2.txt", "r")
o = open("results.txt", "w")
for hash1 in p:
hash1 = hash1.strip('\n')
for data in d:
hash2 = data.split(',')[1].strip('\n')
if hash1 in hash2:
o.write(data)
o.close()
d.close()
p.close()
我期待 32k 个结果。
最佳答案
您的 file2 不太大,因此将其加载到内存中是完全可以的。
- 将 file2.txt 加载到集合中以加快搜索过程并删除重复项;
- 从集合中删除空行;
- 逐行扫描 file1.txt 并将找到的匹配项写入 results.txt。
with open("file2.txt","r") as f:
lines = set(f.readlines())
lines.discard("\n")
with open("results.txt", "w") as o:
with open("file1.txt","r") as f:
for line in f:
if line in lines:
o.write(line)
如果 file2 更大,我们可以将其分割成 block ,并对每个 block 重复相同的操作,但在这种情况下,将结果编译在一起会更困难
关于python - 搜索大文件中的文本并将结果写入文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56192724/