TypeError:int()参数必须是字符串、类似字节的对象或真实的,而不是“NoneType”CSV

qzlgjiam  于 2023-01-28  发布在  其他
关注(0)|答案(1)|浏览(219)
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文件的值并将其生成一个对象

sr4lhrrt

sr4lhrrt1#

你写的

saldo=int(item.get('saldo')),

空的CSV条目将产生一个None值,而int(None)不会让您满意。
有可能默认这样的值,如果在您的用例中有意义的话,可以将其设置为零。

saldo=int(item.get('saldo', '0')),

相关问题