如何使用 Python 在文本文件中查找单词

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/27914069/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 02:27:29  来源:igfitidea点击:

How to find words in text files with Python

pythontext

提问by KMaelstrom

I am new to python and am trying to create a function in python that finds the lines where the word occurs in a text file and prints the line numbers. The function takes the text file name and list of words as input. I don't know where to start.

我是 python 的新手,正在尝试在 python 中创建一个函数,该函数查找文本文件中出现单词的行并打印行号。该函数将文本文件名和单词列表作为输入。我不知道从哪里开始。

Example

例子

index("notes.txt",["isotope","proton","electron","neutron"])

isotope 1
proton 3
electron 2
neutron 5

同位素 1
质子 3
电子 2
中子 5

This is some random code that I made with text; so, I don't know if it can help me or not.

这是我用文本制作的一些随机代码;所以,我不知道它是否可以帮助我。

def index():
    infile=open("test.txt", "r")
    content=infile.read()
    print(content)
    infile.close()

The goal is to be able to find the words in the text file like a person would find a word in the index of a book.

目标是能够在文本文件中找到单词,就像人们在书的索引中查找单词一样。

采纳答案by Hackaholic

try like this:

试试这样:

def word_find(line,words):
    return list(set(line.strip().split()) & set(words))

def main(file,words):
    with open('file') as f:
        for i,x in enumerate(f, start=1):
            common = word_find(x,words)
            if common:
                print i, "".join(common)

if __name__ == '__main__':
    main('file', words)

回答by Adam Smith

words = ['isotope', 'proton', 'electron', 'neutron']

def line_numbers(file_path, word_list):

    with open(file_path, 'r') as f:
        results = {word:[] for word in word_list}
        for num, line in enumerate(f, start=1):
            for word in word_list:
                if word in line:
                    results[word].append(num)
    return results

This will return a dictionary that has all the occurrences of the given word (case-sensitive).

这将返回一个字典,其中包含给定单词的所有出现次数(区分大小写)。

DEMO

演示

>>> words = ['isotope', 'proton', 'electron', 'neutron']
>>> result = line_numbers(file_path, words)
>>> for word, lines in result.items():
        print(word, ": ", ', '.join(lines))
# in your example, this would output:
isotope 1
proton 3
electron 2
neutron 5

回答by stringinator

Adam Smith's answerbroke in Python3.7. I needed to map to a string as follows:

亚当·斯密的回答在 Python3.7 中被打破了。我需要映射到一个字符串如下:

for word, lines in result.items():
    print(word, ": ", ', '.join(map(str,lines)))