我在Python中的類結構內創建了一個類。最後,我嘗試檢索其中一個屬性(price
)到sum
的所有值的列表,並使用它進行數學運算。訪問和列出Python類中的屬性
它一直告訴我,我的班級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)
不應該是'price.append(line.Price)'? – jonrsharpe 2015-02-06 11:14:40