python - 大型文本文件中的超快速正则表达式匹配

标签 python

我希望这段代码能够快速运行。

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/

相关文章:

Python:运行时根据requirements.txt设置PYTHONPATH

python - 使用 Python 从数组中打印 N 次

python - 在 if/while/for 中测试数字或非整数

python - 即使在理解范围之后,列表理解也会重新绑定(bind)名称。这是正确的吗?

python:从 Pandas 中的数据框生成的列表比数据框列长得多

python - RPi3 中的 cv2.VideoWriter 比实际快

python - 无法保存背景减去视频Python openCV

python - 从 Flask api 将参数传递给 Bokeh autoload_server

python - 从 Pandas 的日期时间列中减去一年

python - 访问python中的所有xml值,其中有几个具有相同的名称