Searching Content in File Using Python

Searching Content in File Using Python

Searching Content in File Using Python, In this Article , i will show you how to search content using Python by stepping through the Sample code below.

In order to do this, we will need to setup two things:

  1. The Python Code
  2. The JSON File
  3. Import the Relevant Library here

The Code

 

import json
from difflib import get_close_matches

data = json.load(open("data.json"))

def getData(w):
    w = w.lower()
    if w in data:
        return data[w]
    # if user entered the First Word as Capital Letter
    elif w.title() in data:
        return data[w.title()]
    # if the User enter all words in Upper Case
    elif w.upper() in data:
        return data[w.upper()]
    elif len(get_close_matches(w, data.keys())) > 0:
        ans = input("Did you mean %s instead? Enter Y if yes, or N if not: " % get_close_matches(w, data.keys())[0])
        if ans == "Y":
            return data[get_close_matches(w, data.keys())[0]]
        elif ans == "N":
            return "No such value"
        else:
            return "No Such Entry"
    else:
        return "No such Value."

word = input("Enter word: ")
userOutput = getData(word)
if type(userOutput) == list:
    for item in userOutput:
        print(item)
else:
    print(userOutput)


 

The Explanation

 

  1. To Use Json , First we will need to import the JSON Library
  2. Then we Import the ” difflib import get_close_matches “
    library in order to use the get close matches function , this function will search for the closest result when the User enter an input a wrong data which does not happen to be exist in the database
  3. Open the File “data.json “ and assign the value into the data variable
  4. Create a Function name ” getData ” to Check whether the Data the User input contain inside the the “data.json” File
  5. First convert every single character the data which the User Input in Lower Case w = w.lower()
  6. So if the data the User input exist inside the ” data.json “ File return the Value
  7. else if , then check if the First character of the data which the User has input is an upper Case ” w.title()” and the Value contains inside the “data.json” File. Return the Value
  8. else If then Check whether all character that the User enter are in Upper Case and the value contains inside the “data.json” File. Return the Value
  9. else if Check whether the Data the User Input enter is somewhere near any of the data contains inside the “data.json” File by using the get_close_matches(w, data.keys())) > 0 the Function
  10. By Using the Function above , Python will search for data inside the File which have near similarity to the data the User have Inputed
  11. difflib.get_close_matches(word, possibilities, n, cutoff) accepts four parameters where n and cutoff are optional. word is a sequence for close matches which are desired, possibilities is a list of sequences which able to match the word. Optional argument n (default is 3) is the maximum number of close matches to return, n must be greater than 0. Optional argument cutoff (default 0.6) is a float in the range [0, 1]>
  12. So if there are close match indexes greater than zero
  13. Output and ask the User whether is the suggested data the User is looking for ,.
  14. %s will be replace by the Value % get_close_matches(w, data.keys())[0]) [0] means the program only return the first possibility value that the function output
  15. If the User enter “Y” return the data value
  16. If the User enter “N” Print out ” the Value does not exist
  17. else, if Nothing Satisfies all the condition above ” Output no such value “
  18. Ask the User to enter a word
  19. Assign the word variable that the User enter into the argument of getData(word ) function and then assign the Value into userOutput
  20. Check the type of the userOutput if it is a List loop through and print out the Item
  21. Else if it is a string , Just print out the Item

 
Program to check the User Entry is either string or numerical here

Leave a Reply

Your email address will not be published. Required fields are marked *

2 × one =