简体   繁体   English

Python:检测文本文件中是否只出现一个给定的单词

[英]Python: detect if just one of the given words appear in text file

I am trying to get my program to see if one of the 3 words in "words" appears in the fine kal.txt, just the fact that one of the words appear us enough, but I can't seem to get it working.我试图让我的程序查看“words”中的 3 个词之一是否出现在精细的 kal.txt 中,只是其中一个词出现在我们身上就足够了,但我似乎无法让它工作。

My Code:我的代码:

    textring = open('kal.txt', 'r')

    words  =['flapping', 'Unexpected', 'down']
    len_words = len(words)
    print(len_words)

    counter = 0

    while counter < len_words:
        if words[counter] in textring: 
            print('success')
            SAVE_FILE = open('warnings/SW_WARNING.txt', 'w+')
            SAVE_FILE.write(textring)

        counter += 1

This is the output I get in cmd:这是我在cmd中得到的输出:

    3

That is of course because it's printing the len_words which is 3.那当然是因为它打印的是 3 的 len_words。

Any suggestions why, or does anyone have a solution?任何建议为什么,或者有没有人有解决方案?

First we read the file content.首先我们读取文件内容。 Then we loop over each word and check if it is in the text.然后我们遍历每个单词并检查它是否在文本中。 If it is we write our info and leave the loop.如果是,我们写我们的信息并离开循环。

with open('kal.txt', 'r') as infile:
    text = infile.read()

words = ['flapping', 'Unexpected', 'down']
for word in words:
    if word in text:
        print('success')
        with open('warnings/SW_WARNING.txt', 'w+') as save_file:
            save_file.write(text)
        break

Please note the use of the with context manager.请注意with上下文管理器的使用。 When you use with with open there's no need to close the file after the work is done - a task that you forgot to do in your program.当您使用with with open ,工作完成后无需关闭文件 - 您忘记在程序中执行的任务。

Additional note: A list is iterable.附加说明:列表是可迭代的。 There's no need to use a counter and access the elements with their index.无需使用计数器并通过索引访问元素。 Working with an index is slower, harder to read and so it is considered "unpythonic".使用索引更慢,更难阅读,因此它被认为是“unpythonic”。 Just loop over the values themselves.只需循环值本身。

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

相关问题 查找一个文件上的行是否在 Python 中的另一个文件的行中显示为单词 - Find if lines on one file appear as words in the lines of another file in Python Python - 计算给定文本中的单词 - Python - Count words in a given text Python:如何检查文本文件中是否存在两个或多个给定的单词 - Python: How check if two or more given words are present in a text file 使用Python为文本中的给定单词着色 - Coloring given words in a text with Python 什么是仅读取文本文件中完整单词的 python 代码(词法分析仅检测整个单词)? - What is the python code to read only full words in a text file ( lexical analysis to only detect whole words)? 如果任何人在给定的查询文本中使用同义词,Python Whoosh 库也会给出搜索结果 - Python Whoosh library also give search result if any one using synonyms words in given query text 如何从 Python 中的给定字符串中检测某些单词? - How to detect certain words from a given string in Python? Python - 计算文本文件中的单词 - Python - Counting Words In A Text File Python-如何在文件夹中检测给定类型的文件 - Python - How to detect a file of given type in a folder 检测文本中的英语单词 - Detect english words in text
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM