Created
November 30, 2018 14:39
-
-
Save Ojha-Shashikant/21e7c571f9081cb6a7bd804d1eb4444c to your computer and use it in GitHub Desktop.
Searching from phonebook, based on string input search.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
''' 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