簡體   English   中英

查找並打印包含測試字符串的行

[英]finding and printing the line which contains the test string

我有一個包含以下內容的文本文件(.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

現在我的任務是找出字符串“適合的元素”,然后如果找到該字符串,則需要打印找到它的行。 我可以為此使用re模塊嗎?

是的,但是您不需要。

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM