泡菜:picklingerror:can can can pickle< class' __ main __。

发布于 2025-02-09 03:50:18 字数 1093 浏览 2 评论 0原文

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

第几種人 2025-02-16 03:50:18

您在文件名中有一个错误,腌制文件的命名与您试图取消列出的文件的命名不同,否则它可以按照python 3.7.3和2.7.16的固定来工作,当时名称已修复时:

$ python3 test.py 
serialization is complete - file object created
Deserialization is complete - python obj created
Printing the title details 

TITLE:Battlefield V , PLATFORM:Steam, SIZE:65GB , PRICE:1200
$ python2 test.py 
serialization is complete - file object created
Deserialization is complete - python obj created
('Printing the title details', '\n')
TITLE:Battlefield V , PLATFORM:Steam, SIZE:65GB , PRICE:1200

即使您的原始代码也有不同的错误消息:

$ python3 test.py 
serialization is complete - file object created
Traceback (most recent call last):
  File "test.py", line 27, in <module>
    with open('game.ser', 'rb') as f:
FileNotFoundError: [Errno 2] No such file or directory: 'game.ser'

似乎您的示例代码没有您最初在某些较大应用中遇到的问题。

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:

$ python3 test.py 
serialization is complete - file object created
Deserialization is complete - python obj created
Printing the title details 

TITLE:Battlefield V , PLATFORM:Steam, SIZE:65GB , PRICE:1200
$ python2 test.py 
serialization is complete - file object created
Deserialization is complete - python obj created
('Printing the title details', '\n')
TITLE:Battlefield V , PLATFORM:Steam, SIZE:65GB , PRICE:1200

Even your original code have different error message:

$ python3 test.py 
serialization is complete - file object created
Traceback (most recent call last):
  File "test.py", line 27, in <module>
    with open('game.ser', 'rb') as f:
FileNotFoundError: [Errno 2] No such file or directory: 'game.ser'

It seems that your sample code does not have a problem that you originally encountered in some larger application.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文