泡菜:picklingerror:can can can pickle< class' __ main __。
Pickle无法将数据转储到文件中。我在做什么错?该错误表明 main 上的类属性失败。我已经检查了互联网,但没有太多清晰度。代码如下所示,
import pickle
class Title:
def __init__(self, title, platform, size, price):
self.title = title
self.platform = platform
self.size = size
self.price = price
def display(self):
print("Printing the title details", '\n')
print('TITLE:{} , PLATFORM:{}, SIZE:{} , PRICE:{}'
.format(self.title, self.platform, self.size, self.price))
g = Title("Battlefield V", "Steam", "65GB", "1200")
""" Serialisation to be done of the instance g using pickle module"""
with open('game.csv', 'wb') as f:
pickle.dump(g, f)
print('serialization is complete - file object created')
with open('game.ser', 'rb') as f:
game_obj = pickle.load(f)
print('Deserialization is complete - python obj created')
game_obj.display()
错误消息如下:
pickle.PicklingError: Can't pickle <class '__main__.Title'>: attribute lookup Title on __main__ failed
pickle is not able dump data in file . What am I doing wrong ? The error says that attribute of class on main failed. I have checked internet but not much clarity. The code is as followed
import pickle
class Title:
def __init__(self, title, platform, size, price):
self.title = title
self.platform = platform
self.size = size
self.price = price
def display(self):
print("Printing the title details", '\n')
print('TITLE:{} , PLATFORM:{}, SIZE:{} , PRICE:{}'
.format(self.title, self.platform, self.size, self.price))
g = Title("Battlefield V", "Steam", "65GB", "1200")
""" Serialisation to be done of the instance g using pickle module"""
with open('game.csv', 'wb') as f:
pickle.dump(g, f)
print('serialization is complete - file object created')
with open('game.ser', 'rb') as f:
game_obj = pickle.load(f)
print('Deserialization is complete - python obj created')
game_obj.display()
The error message is as follows:
pickle.PicklingError: Can't pickle <class '__main__.Title'>: attribute lookup Title on __main__ failed
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您在文件名中有一个错误,腌制文件的命名与您试图取消列出的文件的命名不同,否则它可以按照python 3.7.3和2.7.16的固定来工作,当时名称已修复时:
即使您的原始代码也有不同的错误消息:
似乎您的示例代码没有您最初在某些较大应用中遇到的问题。
You have an error in the file name, pickled file is named differently than the one you attempt to unpickle, otherwise it works as it is supposed in both Python 3.7.3 and 2.7.16 when the name is fixed:
Even your original code have different error message:
It seems that your sample code does not have a problem that you originally encountered in some larger application.