Python Search function in .csv using lists

First thing first, I’m the noobest of the noobs… I’m just started to learn coding in python, so I just need the most simple explanation as possible in order to understand things (btw, I hope my english is better than my stupidity in coding)… anyway…
I’ve already tried to search on the net and obviously on StackOverflow but it’s a jungle… similar problems but can’t solve mine.

Basically, 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 works! 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 (and probably is the easiest things to do but… I’m a newbie… sorry!).

Thanks for all the help!

>Solution :

You are headed in the right direction.

Some suggested changes to address the issue you ran into:

  1. Check to see if the user input is acceptable first before bothering to search the csv
  2. Instead of if param in row[0] just do for param == row[0] This fixes the repeated code issue and also solves your "app" != "applebaum" issue
  3. Because you want to see if each column check is also an accepted word for that column then you will need an and

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.

  1. Consider using a list of lists for managing your accepted words. This puts all of the accepted words into a single variable. The index of each inner list would correspond to the columns in your csv.
  2. Create a flag/boolean variable to track if the item was found or not. Then just check that flag at the end of the processing to pop the error message to the user that the item wasn’t found (or wasn’t acceptable).

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()

Leave a Reply