Create timed label if input does not exist - python

I have an entry label where you type a name into. I want it to search through column 0 of the csv file. If that name exists, then it shall be displayed in a listbox that has already been created (this works fine) but if the name does not exist, I want to create a timed label ( the elif section). However, the code I have written displays the label when the name does AND doesn't exist.
with open('CLASSES.csv', 'rt')as f:
reader=csv.reader(f)
people=[]
for column in reader:
if column[0]==entry:
people.append(column[0:8])
namebox.delete(0,END)
nameslist=[x[0] for x in people]
for names in (nameslist):
namebox.insert(END, names)
elif column[0]!=entry:
INC=Label(master,text='User Does Not Exist',bg='#800000',fg='white')
INC.grid(row=2,column=3,columnspan=5,sticky='s')
def af():
INC.configure(fg='#800000')
INC.after(1000, af)
The if column[0] == entry: section works fine but I thought I would include it to make it easier to understand how the program works.
Any help would be great!

You are displaying the record for each entry.
Use a boolean typed variable to store if the entry was found. Initialize it with false before the loop begins. If an entry is equal, set it to true. After the loop check for this value and apply your if branch to it.
Pseudo code
search = "SomeEntryValue"
found = False
for line in open("csv-file", "r"):
if line[0] == search:
found = True
# Optional Tasks to be done here for each entry
if found:
# Display the Data
else:
# display the "User does not exist" message

Related

what is the easiest way to find if a specific value exists in a table with multiple rows on a website using selenium in python?

I am trying to make my script perform a specific action based on the existence of a value in a table row on a website. E.g if x is in row 1 of table 'lab', create investigation, else move to next row and check if x is in that row. Sorry the website I am trying this on is not accessible by those who do not have an account but please see a simpler version of my code to help figure this out. As of now, I am stuck on the second for loop, the code below goes through each row and prints it out but just hangs. Might just be a break I am needing but I have tried it all (break, continue, pass).
#for each patient id in list, find the section in the patients web-table by looking through each row where the covid name and result is stored
#search for results table within a table in a web page for eicrs
elems = driver.find_elements_by_xpath('//*[#id="xmlBlock"]/ul[1]/li/table[1]/tbody')
for idx, elem in enumerate(elems, 1):
for rownum, lab in enumerate(elem.text.split('\n'), 1):
#get lab test from first column for each row
lab_test = text_xpath(f'//*[#id="xmlBlock"]/ul[1]/li[{idx}]/table[1]/tbody/tr[{rownum}]/td[1]')
#get result from second column for each row
lab_result= text_xpath(f'//*[#id="xmlBlock"]/ul[1]/li[{idx}]/table[1]/tbody/tr[{rownum}]/td[2]')
#if lab test is in list of rna tests and positive lab regex, create CONFIRMED investigation
if re.search(covid_test_names, lab_test.lower()) and re.search(pos_regex, lab_result.lower()):
print('Log update: created confirmed investigation')
#else if lab test is in list of antigen tests and positive lab regex, create PROBABLE investigation
elif re.search(ant_regex, lab_test.lower()) and re.search(antigen_pos_regex, lab_result.lower()):
print('Log update: created probable investigation')
else:
print('Log doc: No lab test matches regex', lab_test, lab_result)
continue #continue loop through rows
continue #not sure if needed
break #break out of topmost for loop and move to next line of code once value has been found to match condition.
print('done with that')
If I understand correctly what you need, you should remove both continue from your code and the break at the bottom as well and add a break inside the if and else blocks so if you found the condition you are looking for and performed the action you need - now break the for loop.
Like this:
elems = driver.find_elements_by_xpath('//*[#id="xmlBlock"]/ul[1]/li/table[1]/tbody')
for idx, elem in enumerate(elems, 1):
for rownum, lab in enumerate(elem.text.split('\n'), 1):
#get lab test from first column for each row
lab_test = text_xpath(f'//*[#id="xmlBlock"]/ul[1]/li[{idx}]/table[1]/tbody/tr[{rownum}]/td[1]')
#get result from second column for each row
lab_result= text_xpath(f'//*[#id="xmlBlock"]/ul[1]/li[{idx}]/table[1]/tbody/tr[{rownum}]/td[2]')
#if lab test is in list of rna tests and positive lab regex, create CONFIRMED investigation
if re.search(covid_test_names, lab_test.lower()) and re.search(pos_regex, lab_result.lower()):
print('Log update: created confirmed investigation')
break
#else if lab test is in list of antigen tests and positive lab regex, create PROBABLE investigation
elif re.search(ant_regex, lab_test.lower()) and re.search(antigen_pos_regex, lab_result.lower()):
print('Log update: created probable investigation')
break
else:
print('Log doc: No lab test matches regex', lab_test, lab_result)
has been found to match condition.
print('done with that')
But maybe I do not understand your logic and the question

Try Statement Within for Loop not working python

I have the script below, what I am finding that if my Try statement is satisfied, the whole iteration of the first loop will end and it will just go into the next loop iteration.
What I want to happen is it the, if statement, is satisfied, it then just back to the elif within the for loop that it sits within.
So essentially I want to go
For loop.
Then try if something works (I know some wont)
Then go back into a for loop (and all the subsequent loops will still work.
Ideally I would structure this as: for loop- try statement- for loop, but essentially I am asking the same question as when that happens to, the loop also breaks out.
Thaankyou, Sam
# LOOP OVER THE DICTIONARIES (CSV LINE DATA) AND PULL UP THE PARAMETER KEY TO MATCH
tester = []
updatefamilyname = IN[6]
typename_input01_value = IN[7]
typename_input02_value = IN[8]
for datarow_dict in Data_DictionaryList:
# Get matching value from data row dictionary
Matching_Value = str(datarow_dict[parameter_key_tomatch])
#test.append(parameter_key_tomatch)
#test.append(Matching_Value)
try:
Get_Matching_Value_LongIndex = Base_MatchingWalls_Dict[Matching_Value]
Split_Index = Get_Matching_Value_LongIndex.split(indexplacesplit)[1]
MatchedWall = Base_MatchingWalls[int(Split_Index)]
#test.append(MatchedWall)
#test.append("here2")
for para_key, para_value in datarow_dict.items():
#ResultOutput.append(para_key)
#ResultOutput.append(typename_input01_value)
#test.append(para_key)
#test.append(para_value)
# We then say if the paramter key is the same as the matching key, open the following
if para_key == parameter_key_tomatch:
#test.append("inside")
ResultOutput.append(para_key + " equal to " + para_value)
# Set New Family Type Name Value
#if para_key == typename_input01_key:
#typename_input01_value = para_value
#tester.append("inside link")
#elif para_key == typename_input02_key:
#typename_input02_value = para_value
#else:
#print ("go")
elif para_key != parameter_key_tomatch:
ResultOutput.append(para_key)
print ("not here")
#ResultOutput.append(para_key)
#TestParameter_ = testparavalue(MatchedWall, para_key, para_value)
#else:
#pass
# We then say, if its not the same, this is when we want to do some things, could be where we change the family name if we change the name of the name and number
else:
print ("ouside exception")
#ResultOutput.append("else why")
if updatefamilyname == "Yes":
ResultOutput.append("update name accepted")
print ("yes")
# THIS IS WHERE WE COULD PASS IN THE OTHER ONE TO MATCH TO FORM THE CHANGE IN FAMILY TYPE NAME
#typename_input01_value
#typename_input01_value
# This is where we bring in
# Anything where matching para does not have a corresponding wall type
except:
print ("here")
ResultOutput.append(Matching_Value)

Printing multiple database entries with the same name

sorry, i'm not too sure how to phrase this question.
I am making a database of herbs to be used in cooking etc. and using a python scrip to search the database.
Basically i have multiple entries in the database that have the same name or similar (such as Siberian ginseng and panax ginseng). I would like to print out all of the entries that have the name in them (eg. ginseng), but am not sure how.
this is my code so far:
while True:
herb=input("Herb: ")
database=open("db.txt")
for line in database:
record = line.split('|')
if record[0] == herb:
found = True
break
else:
found = False
continue
if found == False:
print("No herbs in database.")
print('')
else:
print(record[0])
print(record[1])
print(record[2])
print(record[3])
print('')
The output only displays the first entry that has the herb (ginseng) in it, however i want all entries that have the name anywhere in it to be displayed.
Apologies if this question has already been answered or i haven't phrased it right.
It looks like your iterating over your records and when you find an entry that matches you break out of the loop and print it right away.
Something you might want to do is to make a printHerbs function which takes the record array and prints it when its found and not break the loop.
Also only display not found if they reach the end of the loop an nothing was found. It might look something like this:
herb=input("Herb: ")
database=open("db.txt")
def printHerbs(record):
print(record[0])
print(record[1])
print(record[2])
print(record[3])
print('')
found = False;
for line in database:
record = line.split('|')
if herb.lower() in record[0].lower():
found = True
printHerbs(record)
if found == False:
print("Herb not found in database")
Also if you are trying to match if a string is a substring of another string you can use:
if "Ginsing" in "Ginsing Tea":
print("Found")
The .lower() method will make sure both strings are lowercase when they are being compared.

Trouble when trying to check list with another list

One part of my program allows the user to check whether the clues they have entered are correct with the solved version which is in an external file named solved.txt... So far I have the code shown below however it is only showing the three clue pairings at the beginning of the program which are correct, not the ones which I have added throughout the program. I think this is just something minor but I am a bit stuck on what to change.
Here is my code so far...
def check_clues():
# Dictionary to hold all of the symbol/letter combos
coded_dict = {}
# Go through each symbol/letter combination together
with open("words.txt") as fw, open("solved.txt") as fs:
for fw_line, fs_line in zip(fw, fs):
for fw_symbol, fs_letter in zip(fw_line.strip(), fs_line.strip()):
# Add the symbol/letter combination to the dictionary
coded_dict[fw_symbol] = fs_letter
correct_clues = []
with open("clues.txt") as fc:
for fc_line in fc:
# If the symbol is in the dictionary and the letter matches the symbol
if fc_line[1] in coded_dict and coded_dict[fc_line[1]] == fc_line[0]:
# Add a correct clue to your list
correct_clues.append(fc_line.strip())
print("You got a total of {0} correct: {1}".format(len(correct_clues), ", ".join(correct_clues)))
if __name__ == "__main__":
check_clues()
Below is a link to what is in each of the files...
http://www.codeshare.io/vgwrC
If needed, I will add the code for all of my program...

Variable updating one step behind in list searcher in Tkinter

I'm trying to build a search engine that will check a list and then remove all list items that do not meet the search parameters. I know there is several problems with my program such as it will not add things back to the list when you backspace and in my updating for loop I simply tack on a '*' thinking that it will search for strings only beginning with the current parameters, but I will cross those bridges later.
class StudentFinderWindow(Tkinter.Toplevel):
def __init__(self):
Tkinter.Toplevel.__init__(self) # Create Window
searchResultList = ['student1', 'student2', 'student3'] # Test list.
##### window attributes
self.title('Edit Students') #sets window title.
##### Puts stuff into the window.
# text
editStudentInfoLabel = Tkinter.Label(self,text='Select the student from the list below or search for one in the search box provided')
editStudentInfoLabel.grid(row=0, column=0)
# Entry box
self.searchRepositoryEntry = Tkinter.Entry(self)
self.searchRepositoryEntry.grid(row=1, column=0)
# List box
self.searchResults = Tkinter.Listbox(self)
self.searchResults.grid(row=2, column=0)
This fills the Tkinter Listbox with the original list.
# Search results initial updater.
self.getStudentList()
for student in self.studentList:
self.searchResults.insert(Tkinter.END, student)
##### Event handler
Right here I bind to run the list updater after a key is entered into the search box
self.searchRepositoryEntry.bind('<Key>', self.updateSearch)
This is supposed to run every time a key is pressed. It gets the string that is in the Entry then starts a variable count so I know which index the name is at. After that it run a for loop on the current list supposedly checking to see if it fits the requirement of the parameters and any other letter after it. If it does not match it should delete. The problem is the first time I hit a letter the parameters string is just a blank space and then the next letter the string is the first letter and so on. It is always one step behind. And that is the problem
def updateSearch(self, event):
parameters = self.searchRepositoryEntry.get()
int = 0
currentList = self.searchResults.get(0, Tkinter.END)
for i in currentList:
if not i == parameters + '*':
self.searchResults.delete(int)
int += 1
def getStudentList(self):
global fileDirectory # Gets the directory that all the files are in.
fileList = listdir(fileDirectory)
self.studentList = []
for file in fileList:
self.studentList.append(file[:-4])
I believe I have run into this same problem you describe before, when attempting to make an actively searching ctrl-F feature in one of my programs.
What I found to work is not bind on Key but instead KeyRelease. I'm not entirely sure why this works (probably just a quirk with Tkinter). However, it works.
Snippet's:
The binding
# self.FW.En is an entry widget.
self.FW.En.bind('<KeyRelease>', self.find)
Which would run
def find (self, event):
self.Te.tag_remove('found', '1.0', 'end')
pat = self.FW.En.get()
if len(pat) > 1:
index = '1.0'
while True:
index = self.Te.search(pat, index, nocase=1, stopindex='end')
if not index:
break
lastidex = '%s+%dc' % (index, len(pat))
self.Te.tag_add('found', index, lastidex)
index = lastidex
self.Te.tag_config('found', background='#80ff00')

Categories

Resources