运行此代码时,我收到pickle.picklingerror,但不知道为什么。我已经检查过以确保名称没有冲突,并且已经检查了类似的帖子,但出现了此错误,但似乎无法解决问题。
这是3部分程序的部分“ a”,其中outfile将用于部分“ b”和“ c”。
要解决此错误需要更改什么?
如果需要对此信息进行编辑以包含其他信息,我很乐意这样做。
Traceback (most recent call last): File "/home/tarnette/assessment7question1a.py", line 80, in <module> main() File "/home/tarnette/assessment7question1a.py", line 15, in main dictionaryNations() File "/home/tarnette/assessment7question1a.py", line 32, in dictionaryNations pickle.dump(nationDictionary, outfile) _pickle.PicklingError: Can't pickle <class '__main__.Nation'>: it's not the same object as __main__.Nation import pickle file = "UN.txt" infile = open("/home/tarnette/UN/UN.txt", 'r') def main(): dictionaryNations() def dictionaryNations(): nationDictionary = {} for line in infile: data = line.split(',') country = Nation() country.setCountryName(data[0]) country.setContinent(data[1]) country.setPopulation(float(data[2])) country.setArea(float(data[3])) nationDictionary[country.getCountryName()] = country outfile = open("nationsDict.dat", 'wb') pickle.dump(nationDictionary, outfile) outfile.close() return nationDictionary class Nation(): ##class named Nation as defined by textbook, THIS WILL ALSO BE SAVED IN A FILE CALLED "nation.py" def __init__(self): ##first method, is an initializer, first parameter is self self_countryName = "" ##These four variables are the four instance variables that must be created per the project instructions self_continent = "" self_population = 0.0 self_area = 0.0 ##This is the mutator method (change value of instance variable) def setCountryName(self, countryName): self._countryName = countryName ##This is the accessor method (retrieve value of instance variable) def getCountryName(self): return self._countryName ##This is the mutator method (change value of instance variable) def setContinent(self, continent): self._continent = continent ##This is the accessor method (retrieve value of instance variable) def getContinent(self): return self._continent ##This is the mutator method (change value of instance variable) def setPopulation(self, population): self._population = population ##This is the accessor method (retrieve value of instance variable) def getPopulation(self): return self._population ##This is the mutator method (change value of instance variable) def setArea(self, area): self._area = area ##This is the accessor method (retrieve value of instance variable) def getArea(self): return self._area ##This accessor method retrieves value of the calculation made for population density. Titled popDensity per project instructions. def popDensity(self): return (self._population / self._area) main()