Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 查找并打印包含测试字符串的行_Python_Regex - Fatal编程技术网

Python 查找并打印包含测试字符串的行

Python 查找并打印包含测试字符串的行,python,regex,Python,Regex,我有一个包含以下内容的文本文件(.txt): Active nodes=225785 elements=171665 Running protocol file all_in_one.prot" Error reading input protocol file 20 coincident elements Set 999 created (20 elements) All 171665 elements are suitable Error reading input protocol fil

我有一个包含以下内容的文本文件(.txt):

Active nodes=225785 elements=171665
Running protocol file all_in_one.prot"
Error reading input protocol file
20 coincident elements
Set 999 created (20 elements)
All 171665 elements are suitable
Error reading input protocol file
No nodes deleted in SHOW
No elements displayed
Active nodes=225785 elements=171665
Structure disintegrates
Set >ECHCON-Decay< consists of 164361 elements
活动节点=225785个元素=171665
正在运行协议文件all_in_one.prot“
读取输入协议文件时出错
20个重合元素
已创建集合999(20个元素)
所有171665元件均适用
读取输入协议文件时出错
在SHOW中未删除任何节点
没有显示任何元素
活动节点=225785个元素=171665
结构解体
集合>ECHCON衰减<由164361个元素组成

现在,我的任务是找出字符串“elements is applicate”,然后如果找到该字符串,我需要打印找到该字符串的行。我可以使用re模块进行此操作吗?

可以,但不需要

with open('somefile.txt', 'r') as f:
  for line in f:
    if 'elements are suitable' in line:
      print line

是的,谢谢,它可以工作,但这是否适合大数据文件@ignacoi如果你需要它的性能,那么你为什么不首先使用grep?好的,我不知道python中的grep,你能告诉我如何在这里使用它吗?你不会在Pyhon中使用grep.ok你的意思是通过shell使用它…然后我已经使用了…我只是想知道关于re m模块