简体   繁体   中英

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?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM