I have a .csv file divided in four different columns, and I would write a search function in python that gives to me the print of a particular line of the document, but to do that I made some lists to get a thickest search...that's because if I simply open the csv document, the search could find similar word also inside - for instance - inside a name or other words (so, if I try to search 'App' because it is in a column, the result could be the exact line for the word 'App' but also for the name - randomly given for instance - 'Applebaum'... and I don't want that). In my script, I use to give a variable for input() but I need to use tkinter so I think I have to use an Entry instead input()... Finally, I would that if the entry is not equal in the csv file (matching the lists), a messagebox give a message for is missing found.
So, I tried:
import csv
from tkinter import messagebox
def search_parameter():
accepted_word_for_foods = ['bread', 'jam', 'cake', 'wakamole', 'tomato', 'pear']
accepted_word_for_drinks = ['water', 'juice', 'milk', 'tea' ]
accepted_word_for_colors = ['red', 'brown','blue']
param = input('Insert here your param...\n')
csv_f = csv.reader(open('random_list_example.csv', 'r'))
for row in csv_f:
if param in row[0] == accepted_word_for_foods:
print(row)
if param in row[1] == accepted_word_for_drinks:
print(row)
if param in row[2] == accepted_word_for_colors:
print(row)
if param != accepted_word_for_foods:
messagebox.showwarning(title='Not found', message='Param not present or bad digit')
if param != accepted_word_for_drinks:
messagebox.showwarning(title='Not found', message='Param not present or bad digit')
if param != accepted_word_for_colors:
messagebox.showwarning(title='Not found', message='Param not present or bad digit')
search_parameter()
Obviously, it doesn't work! I tried also different combinations (if params == accepted_words_for_foods in row[0]...etc etc...), but I think the problem is in the way I refer to the list, but I don't really understand how to do.
You are headed in the right direction.
Some suggested changes to address the issue you ran into:
if param in row[0]
just do for param == row[0]
This fixes the repeated code issue and also solves your "app" != "applebaum"
issueand
import csv
from tkinter import messagebox
def search_parameter():
accepted_word_for_foods = ['bread', 'jam', 'cake', 'wakamole', 'tomato', 'pear']
accepted_word_for_drinks = ['water', 'juice', 'milk', 'tea' ]
accepted_word_for_colors = ['red', 'brown','blue']
param = input('Insert here your param...\n')
#check to see if the input is acceptable before bothering with the csv
if param not in accepted_word_for_colors and param not in accepted_word_for_drinks and param not in accepted_word_for_foods:
messagebox.showwarning(title='Not found', message='Param not present or bad digit')
else:
#param is acceptable, lets look at the csv
csv_f = csv.reader(open('random_list_example.csv', 'r'))
for row in csv_f:
if param == row[0] and param in accepted_word_for_foods:
print(row)
if param == row[1] and param in accepted_word_for_drinks:
print(row)
if param == row[2] and param in accepted_word_for_colors:
print(row)
search_parameter()
Suggested changes overall.
This would look something like:
import csv
from tkinter import messagebox
def search_parameter():
accepted_words=[['bread', 'jam', 'cake', 'wakamole', 'tomato', 'pear']
,['water', 'juice', 'milk', 'tea' ]
,['red', 'brown','blue']]
param = input('Insert here your param...\n')
#new flag/boolean variable to track if we found the item or not
word_found = False
csv_f = csv.reader(open('random_list_example.csv', 'r'))
for row in csv_f:
#loop through each column using `range()`
for col in range(3):
#since we are looping through the columns now, we only need one line of logic to test each column
if param == row[col] and param in accepted_words[col]:
word_found = True
print(row)
#did that flag get flipped? If not, message the user
if not word_found:
messagebox.showwarning(title='Not found', message='Param not present or bad digit')
search_parameter()