ayaan
ayaan

Reputation: 735

finding and printing the line which contains the test string

i have a text file(.txt) with these contents:

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

now my task is to find out the string "elements are suitable" and then if the string is found i need to print the line in which it was found. can i use re module for this?

Upvotes: 0

Views: 27

Answers (1)

Ignacio Vazquez-Abrams
Ignacio Vazquez-Abrams

Reputation: 799580

Yes, but you don't need to.

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

Upvotes: 2

Related Questions