import csv
class User:
#creates a user
all = []
def __init__(self, name : str, lastname: str,password: str, password2: str, username: str, saldo: int):
#ssert saldo >= 0, "data is negatif that can't be right hu"
self.name = name.capitalize()
self.lastname = lastname.capitalize()
self.saldo = saldo
self.password = password
self.password2 = password2
self.username = username
User.all.append(self)
@classmethod
def instantiate_with_csv(cls):
with open("banking sytem/user.csv",'r') as f:
reader = csv.DictReader(f)
items = list(reader)
for item in items:
User(
name= str(item.get("name")),
lastname=str(item.get("lastname")),
password=str(item.get("password")),
password2=str(item.get("password2")),
username=str(item.get("username")),
saldo=int(item.get('saldo')),
)
User.instantiate_with_csv()
print(User.all)
获取csv文件的值并将其生成一个对象
1条答案
按热度按时间sr4lhrrt1#
你写的
空的CSV条目将产生一个
None
值,而int(None)
不会让您满意。有可能默认这样的值,如果在您的用例中有意义的话,可以将其设置为零。