I have a text file with a series of clients. Each line has a different client. Each client has an ID, a username, and a password.
I want to create a "Client" class, and generate objects in that class in a loop. Each object would have a username and a password, and would be stored in a variable that contains the client's ID. Client 1 would be stored in "client_1", Client 2 would be stored in "client_2", etc.
I created the method "read()" that opens the text file, breaks if there are empty lines, and retrieves the ID, username and password for each client (each line).
What I can't figure out, is how to make it so that when the client's ID is "1", I create an object for that client and store it in the variable "client_1". When the client's ID is "2", I store client's 2 object in the variable "client_2", and so on.
But I want to do this automatically, instead of having 9000 clients and having to create 9000 variables myself.
Thanks
class Client:
def __init__(self, username, password):
self.username = username
self.password = password
def read(self):
clients = []
with open("Clients.txt", "r") as file:
lines = file.readlines()
for line in lines:
if not line:
break
else:
client = line.split(" | ")
client_id = client[0]
#How do I create the variable "client_client[0]"?
username = client[1]
pre_password = client[2]
password = pre_password.strip("\n")
#client_client[0] = Client(username, password)
clients.append(#client_client[0])
return clients
My text file (ID, username, password - from left to right):
1 | admin | Z9?zzz
2 | John | J1!jjj
3 | Steve | S1!sss
Also, is there a problem if I'm using the "username" and "password" variables in read(), when I have already used them in the def init?
Thanks