Home > Software engineering >  Searching through a list of text files for a full name, ignoring whitespace
Searching through a list of text files for a full name, ignoring whitespace

Time:04-01

I am very new to Python and I am making a simple program for me and my friends. It is a menu that has 4 options, one of them being to search for one of us. It is an input that asks the user what name they want to search for, and then will display the text file for that user. When the option is selected, the user will be prompted with "Enter a name: ", and then they must enter the first and second name of the person they want to search for.

What I am trying to do is allow the user to search for the person they want by searching for the first and last name, which would ignore the white space from the text file.

What I have done so far is, I've made it so the user is able to search for someone, but this does not ignore whitespace. So, for example, if I wanted to search for "Homer Simpson", I could search for Homer and find the result, but if I search for "Homer Simpson", it will give me "Invalid option" from my menu. I am trying to change that.

Here is the code that I am using for the option:

import os
from os import listdir
os.system("")
import glob
import os.path

def menu():
    print("1. View a user")

menu()
option = int(input("\n"   "Enter your option: "))

while option !=0:
    if option == 1:
        user = input("Enter a name: ")
        dir_path = r'D:\My project\users'
        #The user folder contains all users named as their name followed by Txt, e.g. Homer Simpson.txt
        for file in os.listdir(dir_path):
            cur_path = os.path.join(dir_path, file)
            if os.path.isfile(cur_path):
                with open(cur_path, 'r') as file:
                    contents = file.read()
                    if user in contents:
                        print(contents   '\n')
                        #Printing the contents of the text file that the name has been found in
                        menu()
                        break
                    else:
                        print ("Invalid option")

So far, my thinking has been, in order to remove spaces, when the file is read, I would need to remove spaces completely, as well as removing spaces from the users' input. It would then search trhough the text files for the user's input without whitespace, finding a match in one of the text files, and then restoring the text file back to its original when the text file is printed, although I am not too sure how to do this.

Any help would be much appreciated, thanks!

CodePudding user response:

If I am understanding your problem correctly you want to see if the name entered, without spaces, is contained within the text contents of a file, without the spaces as well. If this is correct then you can use the re library for this. The re library is regular expressions. So you will first import re in your program.

Then you can change your line:

if user in contents:

To:

if re.sub('\s','',user) in re.sub('\s','',contents):

The function re.sub(pattern,replaced_with,original_string) will replace any instance of a desired pattern in a string and return a modified string, while leaving the original string unchanged. In this case the string '\s' is a regular expression for any whitespace character (tabs, new lines, spaces...). So re.sub('\s','',content) will return a version of your variable content where any whitespace is replaced by an empty string.

  • Related