Lab 3: Databased Admin Tool Python
Objective: Develop an admin tool for username and password management. You will need to use the best data structure to store the data you read from UD.txt for efficient processing. You can accomplish these tasks with Dictionary or List.
The data file (UD.txt) :
FIRST NAME, LAST NAME,USERNAME,PASSWORD
Sam, DDal,sdd233,Pad231
Dave,Dcon,dcf987, BHYW4fw
Dell,Grant,dgr803,Sb83d2d
Mike,Kress,mkr212,UNNHS322
Lisa,Kate,lki065,dgw6234
Paul,Edward,ped332,9891ds
Youyou,Tranten,ytr876,dsid21kk
Nomi,Mhanken,nmh223,3282jd3d2
Write a program that imports the database from UD.txt
Give the following options:
A: Search by last name
B: Search by first name
C: Search by username
D: Display all users alphabetically by First name
E. Display all users alphabetically by Last name
F: Insert a user
If the user is found, display the user’s info. For example: Sam, DDal,sdd233,Pad231
If user not found, display “Not found”, and return the user to main menu
If the user is found, display the user’s info. For example: Sam, DDal,sdd233,Pad231
If user not found, display “Not found”, and return the user to main menu
If the user is found, display the user’s info. For example: Sam, DDal,sdd233,Pad231
If user not found, display “Not found”, and return the user to main menu
Important: Declare one function called “def output ()” to output information for option D and E.
source code:
#Please go through comments and modify if any thing required thank you
#screenshots provided for indentation and reference purposes
#output function for D and E options
#prints the list if provided
def output(data):
print('{0: <20}{1: <20}{2: <20}{3: <20}'.format("FIRST
NAME","LAST NAME","USERNAME","PASSWORD"))
for ml in data:
print('{0: <20}{1: <20}{2: <20}{3:
<20}'.format(ml[0],ml[1],ml[2],ml[3]))
#main function
def main():
file=open("UD.txt","r") #text file opening
ml=list()
i=0
for line in file: #reading line by line and listing all contents as
each list
if i!=0:
ll=line.split(",")
ml.append(ll)
i=1
file.close() #closed text file
print("Select from following options:\n") #user given with
options
print("A: Search by last name\nB: Search by first name\nC: Search
by username\nD: Display all users alphabetically by First name\nE.
Display all users alphabetically by Last name\nF: Insert a
user\n")
ch=input("Q for quit.")
while(True):
if ch=='Q':#quits
break;
elif ch=='A':#searches content using last name
name=input("Enter last name to Search: ")
i=0
j=0
for ll in ml:
if ll[1]==name:
i=j
break;
j=j+1
if i==0:
print("Not Found")
else:
print('{0: <20}{1: <20}{2: <20}{3:
<20}'.format(ml[i][0],ml[i][1],ml[i][2],ml[i][3]))
elif ch=='B':#searches name using first name
name=input("Enter first name to Search: ")
i=0
j=0
for ll in ml:
if ll[0]==name:
i=j
break;
j=j+1
if i==0:
print("Not Found")
else:
print('{0: <20}{1: <20}{2: <20}{3:
<20}'.format(ml[i][0],ml[i][1],ml[i][2],ml[i][3]))
elif ch=='C':#searches using usrname
name=input("Enter username to Search: ")
i=0
j=0
for ll in ml:
if ll[2]==name:
i=j
break;
j=j+1
if i==0:
print("Not Found")
else:
print('{0: <20}{1: <20}{2: <20}{3:
<20}'.format(ml[i][0],ml[i][1],ml[i][2],ml[i][3]))
elif ch=='D':#sorts the data using first name
rl=ml
rl.sort(key=lambda x:x[0])
output(rl)
elif ch=='E':#sorts data using last name
rl=ml
rl.sort(key=lambda x:x[1])
output(rl)
elif ch=='F':#writest to text file
det=input("Enter user info in the following format: First name,
last name, username, password:")
file=open("UD.txt","a")
file.write("\n")
file.write(det)
file.close
print("Select from following options:\n")
print("A: Search by last name\nB: Search by first name\nC: Search
by username\nD: Display all users alphabetically by First name\nE.
Display all users alphabetically by Last name\nF: Insert a
user\n")
ch=input("Q for quit.")
if __name__ == '__main__':
main()







Lab 3: Databased Admin Tool Python Objective: Develop an admin tool for username and password management....