【问题标题】:How to search for a word in a txt file, then print the line that contains that word(Python)?如何在 txt 文件中搜索一个单词,然后打印包含该单词的行(Python)?
【发布时间】:2018-08-02 15:17:28
【问题描述】:

有谁知道我怎样才能做到这一点?
非常感谢您的帮助!

【问题讨论】:

标签: python search


【解决方案1】:

非常基本的方法:

with open("file.txt") as file:
    for line in file.readlines():
        if "text" in line:
            print line

【讨论】:

    猜你喜欢
    • 2021-02-13
    • 1970-01-01
    • 2023-03-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多