我希望这段代码能够快速运行。
import re
with open('largetextfile.txt') as f:
for line in f:
pattern = re.compile("^1234567")
if pattern.match(line):
print (line)
需要 19 秒。
我修改了它:
import re
with open('largetextfile.txt') as f:
for line in f:
if "1234567" in line:
pattern = re.compile("^1234567")
if pattern.match(line):
print (line)
需要 7 秒。
那么问题来了,有没有更好的办法呢?
我从社区得到了两个想法,并基于此我在以下网址提出了详细问题:https://codereview.stackexchange.com/questions/135159/python-search-for-array-in-large-text-file
最佳答案
检查这是否符合您的要求:
with open('largetextfile.txt') as f:
for line in f:
if line.startswith('1234567'):
print line
关于python - 大型文本文件中的超快速正则表达式匹配,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38410982/