在大型文本文件中搜索字符串的廉价方法 [英] Cheap way to search a large text file for a string

查看:38
本文介绍了在大型文本文件中搜索字符串的廉价方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要在一个非常大的文本文件中搜索特定字符串.它是一个包含大约 5000 行文本的构建日志.这样做的最佳方法是什么?使用正则表达式应该不会引起任何问题吗?我将继续阅读行块,并使用简单的查找.

I need to search a pretty large text file for a particular string. Its a build log with about 5000 lines of text. Whats the best way to go about doing that? Using regex shouldn't cause any problems should it? I'll go ahead and read blocks of lines, and use the simple find.

推荐答案

如果是相当大"的文件,则依次访问各行,不要将整个文件读入内存:

If it is "pretty large" file, then access the lines sequentially and don't read the whole file into memory:

with open('largeFile', 'r') as inF:
    for line in inF:
        if 'myString' in line:
            # do_something

这篇关于在大型文本文件中搜索字符串的廉价方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆