2

これがすでに他の場所で回答されている場合は、お詫び申し上げます。インターネットを見回しましたが、明確な答えが見つかりませんでした

クラス定義(いくつかの値とメソッドを含む)と、リストに保持されているクラスの複数のインスタンス化があります。(各リストエントリはインスタンス化です。)

リストをピクルスにしようとすると、「pickle.PicklingError」例外が発生します。これにより、一部のオブジェクトは「ピクルス可能」ではないことがわかりましたが、私の単純なリストは問題ないようです。

ピクルスできないオブジェクトはどれですか?

これがピクルスを行う実際のコードです。(このコードは、クラス内で定義されたメソッドであり、ピクルスにする必要のあるクラスオブジェクトも含まれています。これは問題の一部ですか?)

def Write_Transaction_History_To_File(self):
    if (self.Transaction_History == True): # if History is not empty

        filename = self.Transaction_Name + '_Transaction_History.bin'
        f = open(filename, 'w')

        try:
            pickle.dump(self.Transaction_History , f, -1)   #use highest protocol
        except pickle.PicklingError:
            print 'Error when serializing data'
        f.close()

    else:
        print 'No History to store'
4

1 に答える 1

3

モジュールスコープにないネストされたクラスをピクルスしようとすると、問題が発生します。

ドキュメントから:

The following types can be pickled:


None, True, and False
integers, long integers, floating point numbers, complex numbers
normal and Unicode strings
tuples, lists, sets, and dictionaries containing only picklable objects
functions defined at the top level of a module
built-in functions defined at the top level of a module
classes that are defined at the top level of a module
instances of such classes whose __dict__ or the result of calling __getstate__() is 
picklable (see section The pickle protocol for details).
于 2013-01-07T21:54:04.853 に答える