Search a text file and print related lines in Python?

后端 未结 3 1361
长情又很酷
长情又很酷 2020-11-29 21:56

How do I search a text file for a key-phrase or keyword and then print the line that key-phrase or keyword is in?

3条回答
  •  自闭症患者
    2020-11-29 22:06

    with open('file.txt', 'r') as searchfile:
        for line in searchfile:
            if 'searchphrase' in line:
                print line
    

    With apologies to senderle who I blatantly copied.

提交回复
热议问题