我在 Python 的类结构中创建了一个类。最后,我尝试将其属性之一 ( price
) 的列表检索到sum
所有值并对其进行数学运算。
它一直告诉我,我的班级TOOLBOX
或班级DATA
都没有属性Price
。我怎么能解决这个问题?
我的代码如下所示:
class DATA:
def __init__(self, Identifier, Price, Date, Postcode, Type, Age, Tenure, Primary, Secondary, Street, Locality, Town, District, County, Status):
self.Identifier = Identifier
self.Price = Price
self.Date = Date
self.Postcode = Postcode
self.Type = Type
self.Age = Age
self.Tenure = Tenure
self.Primary = Primary
self.Secondary = Secondary
self.Street = Street
self.Locality = Locality
self.Town = Town
self.District = District
self.County = County
self.Status = Status
class TOOLBOX(object):
def __init__ (self):
self.alldata = []
def add_data(self, Identifier, Price, Date, Postcode, Type, Time, Tenure, Primary, Secondary, Street, Locality, Town, District, County, Status):
self.alldata.append(DATA(Identifier, Price, Date, Postcode, Type, Time, Tenure, Primary, Secondary, Street, Locality, Town, District, County, Status))
def get_prize(self) :
price=[]
for line in self.alldata:
price.append(self.alldata.Price)
print price
def summation(self):
return sum(self.alldata.Price)
csv_ff = csv.reader(open("FINAL.csv",'rU'))
l=len(list(csv.reader(open("FINAL.csv",'rU'))))
dd = TOOLBOX()
for line in csv_ff:
if len(line)==15:
Identifier=line[0]
Price=int(line[1])
Date=line[2]
Postcode=line[3]
Type=line[4]
Age=line[5]
Tenure=line[6]
Primary=line[7]
Secondary=line[8]
Street=line[9]
Locality=line[10]
Town=line[11]
District=line[12]
County=line[13]
Status=line[14]
dd.add_data(Identifier, Price, Date, Postcode, Type, Age, Tenure, Primary, Secondary, Street, Locality, Town, District, County, Status)