petrbel petrbel - 4 months ago 6x
Python Question

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

I need to

an object that stores an instance of
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!


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.