繁体   English   中英

如何在文本文档中搜索单词并显示该行?

[英]How do I search for a word in a text document and display that line?

这就是我现在所拥有的。 我需要协助。 我是python的新手,并没有太多的知识。 我需要在文件中搜索某个单词,然后当该单词出现在该行中时,它会打印出来。

file = open(input("please input your file name   "))
        word = input("please enter the value you want to search for   ")

    with open(file, 'r') as file:
        for line in file:
            if 'word' in line:
                print (line)

我遇到的问题是理解我需要分配给with语句的内容。 找到单词和行。 目前我在with语句中将文件作为文件。 我不知道还能放什么。

它应该是(请注意,没有引号):

if word in line:

if 'searchphrase' in line:

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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