gpt4 book ai didi

python - pickle 加载错误

转载 作者:行者123 更新时间:2023-12-01 09:13:52 29 4
gpt4 key购买 nike

这是我的代码

  #my process class----------
class Process(object):

def PrintName(self, name):
print('Your name is : ', name)
#pickling-------------
import pickle
model = Process()
filename = 'Process.pkl'
pickle.dump(model, open(filename, 'wb'))

#loading the pickle-------------

model = pickle.load(open('Process.pkl', 'rb'))

当我在 jupyter 笔记本上运行上面的代码时,我收到错误 AttributeError: 'module' object has no attribute 'Process',混淆哪一行导致错误

如有任何帮助,我们将不胜感激

最佳答案

在Python中,缩进很重要。由于缩进错误,函数后面的所有内容仍然是 Process 类的一部分。

您可以阅读有关 python 缩进和编码风格的更多信息 here

我根据 PEP8 为您格式化了您的代码,它现在应该可以工作:

import pickle


# my process class----------
class Process(object):

def PrintName(self, name):
print('Your name is : ', name)


# pickling-------------
model = Process()
filename = 'Process.pkl'
pickle.dump(model, open(filename, 'wb'))

# loading the pickle-------------

model = pickle.load(open('Process.pkl', 'rb'))

关于python - pickle 加载错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51416302/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com