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:
- The Python Code
- The JSON File
- 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
- To Use Json , First we will need to import the JSON Library
- 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 - Open the File “data.json “ and assign the value into the data variable
- Create a Function name ” getData ” to Check whether the Data the User input contain inside the the “data.json” File
- First convert every single character the data which the User Input in Lower Case w = w.lower()
- So if the data the User input exist inside the ” data.json “ File return the Value
- 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
- 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
- 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
- By Using the Function above , Python will search for data inside the File which have near similarity to the data the User have Inputed
- 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]>
- So if there are close match indexes greater than zero
- Output and ask the User whether is the suggested data the User is looking for ,.
- %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
- If the User enter “Y” return the data value
- If the User enter “N” Print out ” the Value does not exist
- else, if Nothing Satisfies all the condition above ” Output no such value “
- Ask the User to enter a word
- Assign the word variable that the User enter into the argument of getData(word ) function and then assign the Value into userOutput
- Check the type of the userOutput if it is a List loop through and print out the Item
- 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