Skip to content

Instantly share code, notes, and snippets.

@Ojha-Shashikant
Created November 30, 2018 14:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Ojha-Shashikant/21e7c571f9081cb6a7bd804d1eb4444c to your computer and use it in GitHub Desktop.
Save Ojha-Shashikant/21e7c571f9081cb6a7bd804d1eb4444c to your computer and use it in GitHub Desktop.
Searching from phonebook, based on string input search.
''' Searching from phonebook, based on string input search '''
def file_input():
filename = input("Please enter filename: ")
return filename
def input_string():
char_input = input("Please enter string you are searching for: ")
return char_input.lower()
def reading_file(filename):
with open("C:\\Users\\Ojha\\Documents\\Python exercises\\Python Scripts\\" + filename, 'r+') as FH:
text = FH.read()
return text
def file_data_operation(text, char_input):
lines = text.split('\n')
users = dict()
for line in lines:
line = line.lower()
if char_input in line:
user_data = line.split(':')
users[user_data[0].capitalize()]= user_data[1]
return users
def sorting_users(users):
names = list(users.keys())
names.sort()
print("Your search in ascending order is: ")
for name in names:
print(name + ':' + users[name])
#main starts here
if __name__ == '__main__':
filename = file_input()
char_input = input_string()
text = reading_file(filename)
users = file_data_operation(text, char_input)
sorting_users(users)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment