-1
我想将类的实例存储到文件中,然后读取数据并根据特定属性以升序显示。根据特定属性对类进行排序
Description of the class: vehicle
Attributes: vehicle type, fuel type, cost
Methods: GetVdata(), ShowVdata()
我一直无法根据成本要求对输出数据进行排序。
import pickle
class vehicle:
vehicletype = ''
fueltype = ''
cost = 0
def __init__(self, vehicletype = '', fueltype = '', cost = 0):
self.vehicletype = ''
self.fueltype = ''
self.cost = 0
def GetVdata(self):
self.vehicletype = raw_input("enter the vehicle type: ")
self.fueltype = raw_input("enter the fuel type: ")
self.cost = input("enter the cost of the vehicle: ")
def ShowVdata(self):
print "vehicle type : ", self.vehicletype, "\n" , "fuel type: ", self.fueltype , "\n", "cost : ", self.cost
#v = vehicle()
#v.GetVdata()
#v.ShowVdata()
dat1 = vehicle("4 wheeler", "diesel" , 2000000)
dat2 = vehicle("2 wheeler", "petrol" , 100000)
dat1.GetVdata()
dat2.GetVdata()
File = open('transport.dat' , 'wb')
pickle.dump(dat1, File)
pickle.dump(dat2, File)
File.close()
dat1 = vehicle()
File = open('transport.dat','rb')
try:
while True:
print"\n"
output = pickle.load(File)
output.ShowVdata()
except EOFError:
File.close()
非常感谢!这是一个很大的帮助。 – nerd0711