I took an implementation of a function that getting this array: [key1, key2, key3]
My function need to check if all the keys are exist in file.
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]
please help me fix my function, many thanks:
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
CodePudding user response:
first off: This is my first stackoverflow-post and I'm not a python pro, but let me see if I can help.
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. But that of course depends on your file as well.
Best
CodePudding user response:
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))
CodePudding user response:
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
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
CodePudding user response:
First of all, you should be passing the filename and list of keys as parameters to your function for reusability.
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.
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