Python 如何在文本文件的哪一行上找到单词

Python 如何在文本文件的哪一行上找到单词,python,Python,我有一个包含以下内容的文本文件: Pacemakers, neurotechnology, nanoparticles, and interpretation of medical images - with the combination of knowledge in mathematics, physics, and technology, you can be involved in developing medical technology that can

我有一个包含以下内容的文本文件:

    Pacemakers, neurotechnology, nanoparticles, and interpretation of medical images - 
    with the combination of knowledge in mathematics, physics, and technology, 
    you can be involved in developing medical technology that can save lives and improve human 
    health.



    Developments in medical technology are going furiously fast and "engineering for health" 
    is becoming increasingly important. As a civil engineer in medical technology, 
    you get to participate in the development of medical science. 
    You create the tools that process data from computed tomography, MRI cameras, and bio-optical 
    sensors into a form that the doctor can interpret; which combines technology with the body's 
    functions and which explores new areas of application in health, environment, and sports.
我试图做的是编写一个搜索单词的代码,输出结果将是代码所在的行。例如,如果我想搜索“起搏器” 然后输出将是“1”,因为它位于该行上,依此类推

我在想,也许代码可以查找文件中的每个“\n”,只要找到它,就会将计数器增加1,这样我就可以跟踪哪一行。 当找到这个词时,我只返回计数器的值

我试过这样的方法:

def find_行(word):
计数器=0
将open(self.text,“r”,encoding=“utf-8”)作为_文件:
对于_文件中的行:
如果word==a_文件:
如果关键字==“\n”:
计数器+=1
然而,我觉得这个逻辑完全是倒退的,我真的不知道如何从这里开始。

使用

以open(self.text,“r”,encoding=“utf-8”)作为_文件的
:
对于行号,枚举中的行(一个文件,开始=1):
如果单词对齐:
打印(行号)

您已经把这个问题复杂化了。 假设您已通读该文件,并将每一行存储在名为行的列表中。使用
enumerate()
来实现您的目标将很容易

rows = [row for row in file]
for count,row in enumerate(rows):
    if 'Pacemakers' in row:
        print(count)

在这种情况下,您可以采取多种方法

  • 您可以逐行检查当前行中是否存在该单词:
  • 如果您想尝试多个单词,可以一次读取这些行并在该列表上执行搜索,每次读取文件都会使代码变慢:

  • 每个人都在告诉你使用枚举。它们没有错,但是您想知道您的代码出了什么问题吗?它需要打印,每行的计数应该无条件地增加。查看行中的每个单词也有帮助

    def find_row(word):
        counter = 0
        with open(self.text,"r",encoding="utf-8") as a_file:
            for line in a_file:
                if word in line:
                    print(counter)
                counter += 1
    
    这将自己起作用。当然,enumerate会把事情整理好,但知道自己怎么做很好

    我知道你说过要回来。但你也谈到了产出。这不是一回事。无论如何,您可以将
    打印
    替换为
    返回
    。但这只会告诉你匹配的第一行号码。使用
    print
    将输出匹配的每个行号


    很难说哪一个是正确的,因为您的示例和描述都考虑了这两种情况。

    正如建议的那样,有几种方法可以做到这一点

    我会做的更简单的解决方案是

    lines = open(path,"r",encoding="utf-8").readlines()
    for line in lines:
        if(word in line):
            print(lines.index(line) + 1)
    


    PSST!-嘿,如果你在这行没有找到你的单词的情况下计算新行,效果会更好。我试着写一个类似的代码,但是,在尝试了你的解决方案后,它说“关键字”没有定义。我应该用别的东西来代替它吗?复制过去的错误。现在好了吗?不是真的,我不知道是不是我,但不管我把什么作为“单词”输入,它只会给我0作为输出。我猜不是所有的单词都在第一排。例如,单词“Developments”也表示0。我将“word”替换为我想要查找的实际单词,self.text是我想要检查的文本。@HåkanMjölk我怀疑word永远不会得到“\n”。这是怎么回事?是的,有点像,但是,在像“a”这样的词中,输出说了十次,而实际上它在文本中只出现了两次。也许它也计算包含“a”的单词?
    def find_row(word):
        counter = 0
        with open(self.text,"r",encoding="utf-8") as a_file:
            for line in a_file:
                if word in line:
                    print(counter)
                counter += 1
    
    lines = open(path,"r",encoding="utf-8").readlines()
    for line in lines:
        if(word in line):
            print(lines.index(line) + 1)
    
    lines = open(path,"r",encoding="utf-8").readlines()
    for line_index in range(len(lines)):
        if(word in lines[line_index]):
            print(line_index + 1)