本文介绍了在Python中有效提取包含字符串的行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有时我只需要从文本文件中获取包含特定字符串的行(例如,在解析日志文件时).我通常是这样做的:

Sometimes I need to get only lines containing a certain string from a text file (e.g., while parsing a logfile). I usually do it this way:

with open(TEXTFILENAME,'r') as f:
  contents = f.readlines()

targets = [s for s in contents if FINDSTRING in s]

但是,我看到可能有两行:

However, I saw there's a possible two-liner:

with open(TEXTFILENAME,'r') as f:
  targets = [s for s in f.readlines() if FINDSTRING in s]

我想知道第二种方法是否更有效,在这种情况下 readlines() 函数是否充当各种迭代器.

I wonder if the second method is more efficient, whether the readlines() function in this case act as an iterator of sorts.

推荐答案

避免调用 readlines,它会生成所有行的列表.因此这应该更快

Avoid the call to readlines, which generates a list of all the lines. This should therefore be faster

with open(TEXTFILENAME,'r') as f:
    targets = [line for line in f if FINDSTRING in line]

这篇关于在Python中有效提取包含字符串的行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-22 09:58