petrbel petrbel - 5 months ago 14
Python Question

Why can't I pickle an instance of unicodecsv.reader?

I need to

cPickle.dump
an object that stores an instance of
unicodecsv.reader
class. When I try to do so, the process terminates with exception:

PicklingError: Can't pickle <type '_csv.reader'>: it's not the same object as _csv.reader


I've found that this might be problem when you have multiple different imports of the same object but that's not my case.

Could you give me a hint on what's going on? Thanks!

Answer

Your problem is that csv.reader objects can't be pickled so unicodecsv.reader objects can't be pickled either. It doesn't normally make sense to pickle csv.reader objects because they're normally used with files, which also can't be pickled.

There's no simple solution to your problem. You could either define your own custom pickling methods that tell's the pickler how to pickle the object without pickling the csv.reader object, or you can rewrite the implementation of the object so it doesn't need to store an instance of unicodecsv.reader.

Comments