8. Create a CSV file by entering user-id and password, read and search the password for given userid.

 8. Create a CSV file by entering user-id and password, read and search the password for given userid.

Python

importcsv

 

defcreate_csv_file(filename):

with open(filename, 'w', newline='') as file:

writer = csv.writer(file)

writer.writerow(["User ID", "Password"])

for _ in range(3):  # Example: Create 3 user entries

user_id = input("Enter User ID: ")

password = input("Enter Password: ")

writer.writerow([user_id, password])

 

defsearch_password_in_csv(filename, user_id):

with open(filename, 'r') as file:

reader = csv.reader(file)

next(reader)  # Skip header row

for row in reader:

if row[0] == user_id:

return row[1]

return None

 

if __name__ == "__main__":

filename = "users.csv"

create_csv_file(filename)

user_id_to_search = input("Enter User ID to search: ")

password = search_password_in_csv(filename, user_id_to_search)

if password:

print(f"Password for {user_id_to_search}: {password}")

else:

print("User ID not found.")

No comments:

Post a Comment