泡菜加载错误

Pickle loading error

这是我的代码

  #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 notebook 上 运行 以上代码时,我得到一个错误 AttributeError: 'module' object has no attribute 'Process', 混淆哪一行导致错误

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

在Python中,缩进很重要。 由于缩进错误,您的功能之后的所有内容仍然是 class 流程的一部分。

您可以阅读更多关于 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'))