小编dan*_*lor的帖子

高效搜索海量文件中的字符串

我发现了这个想法的变体,但没有一个可以让我(对 python 非常陌生)到达我需要的地方。

这是场景:

  1. 我有一个巨大的 27 演出,hashfile.txt由单独的字符串组成。
  2. 我需要逐行解析这个文件,在另一个不太大(~800mb)的addresses.txt文件中搜索匹配项
  3. 当找到匹配时,这需要写入 outfile.txt

我当前的代码已尽我所能优化,但只有 150 行/秒左右。考虑到我有超过 15 亿行hashfile.txt,任何优化都会有所帮助。

fin = 'hashed.txt'
nonzeros = open('addrOnly.txt', 'r')
fout = open('hits.txt', 'w')
lines = nonzeros.read()
i = 0
count = 0

with open(fin, 'r') as f:
    for privkey in f:
            address = privkey.split(", ")[0]
            if address in lines:
                    fout.write(privkey)
            i = i+1
            if i%100 == 0:
                    count = count + 100
                    print "Passed: " + str(count)
Run Code Online (Sandbox Code Playgroud)

python optimization search large-files

4
推荐指数
2
解决办法
3011
查看次数

标签 统计

large-files ×1

optimization ×1

python ×1

search ×1