简体   繁体   English

在 python 的文件中搜索单词

[英]searching words in file in python

I took an implementation of a function that getting this array: [key1, key2, key3]我实现了一个 function 得到这个数组:[key1,key2,key3]

My function need to check if all the keys are exist in file.我的 function 需要检查文件中是否存在所有密钥。

each line in file contain just one of the keys [not all of them]文件中的每一行只包含一个键[不是全部]

I printed my function in order to check, and it seems that it's trying to search all the keys in each line [which of course always be FALSE]我打印了我的 function 以进行检查,似乎它正在尝试搜索每行中的所有键[当然总是错误的]

please help me fix my function, many thanks:请帮我修复我的 function,非常感谢:

def is_all_keys_in_file():
    with open(path_to_file, "r") as file:
        key1 = '0\n'
        key2 = 'name'
        key3 = 'RecipeEndSucceeded'
        keys = [key1, key2, key3]
        counter = 0

        for line in file:
            for key in keys:
                if key in line:
                    counter += 1

    if counter == len(keys):
        return True
    else:
        return False

first off: This is my first stackoverflow-post and I'm not a python pro, but let me see if I can help.首先:这是我的第一个 stackoverflow-post,我不是 python 专业人士,但让我看看我能不能帮忙。

def run():
    found_keys = []
    filepath = r"C:\Users\Path_to_the_file\File.txt"
    keylist = ["key1", "key2", "key3"]
    with open(filepath, "r") as f:
        lines = f.readlines()
        for line in lines:
            for key in keylist:
                if key in line:
                    if key not in found_keys:
                        found_keys.append(key)
    found_all = True
    for key in keylist:
        if key not in found_keys:
            found_all = False
    print(found_all)
    

This filepath points to a text file with the following content:此文件路径指向具有以下内容的文本文件:

Hello this is my testfile
0123
key3 I dont know
What
key2 whatever
should
key1 hm well
something 
foo
bar

For me, the run() function now prints True if all keys are found and False if not.对我来说,run() function 现在打印 True 如果找到所有键,则打印 False 如果没有。 But that of course depends on your file as well.但这当然也取决于您的文件。

Best最好的

You could do something like that:你可以这样做:

from pathlib import Path
txt = Path('data.txt').read_text()
keys = [ '0\n', 'name', 'RecipeEndSucceeded']

print(all(key in txt for key in keys))

Well, instead of scanning the file with loop, I defined three variables that each one reads a line in file and I checked if my key in array,= from the line contant, it fixed my issue好吧,我没有使用循环扫描文件,而是定义了三个变量,每个变量都读取文件中的一行,然后我检查了我在数组中的键是否,= 从行 contant,它解决了我的问题


def is_all_keys_in_file():
    with open(path_to_file, "r") as file:
        key1 = '0\n'
        key2 = 'name'
        key3 = 'RecipeEndSucceeded'
        keys = [key1, key2, key3]

        status_index = metro_callback.readline()
        recipe_name = metro_callback.readline()
        metrology_status = metro_callback.readline()
        if status_index == key1 and recipe_name == key2 and metrology_status == key3:
            return True
        return False

First of all, you should be passing the filename and list of keys as parameters to your function for reusability.首先,您应该将文件名和密钥列表作为参数传递给 function 以实现可重用性。

What you also want is the possibility of an "early" termination once (if) all keys have been observed.您还想要的是一旦(如果)所有键都已被观察到,则有可能“提前”终止。 Another answer shows how you could use all() which is fine but is potentially inefficient for large files although it does make the code more concise.另一个答案显示了如何使用all()这很好,但对于大文件可能效率低下,尽管它确实使代码更简洁。

Here's a step-by-step approach:这是一个循序渐进的方法:

def is_all_keys_in_file(filename, keys):
    s = set(keys)
    with open(filename) as data:
        for line in data:
            if len(s) == 0: # all keys have been observed
                break
            for key in s:
                if key in line:
                    s.remove(key)
                    break
    return len(s) == 0

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

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