How I can deserialize python pickles in C#?

11,504

Solution 1

You say you can't change the program that generates the pickle. But surely you can write a separate Python program to read the pickle and write it out again as JSON?

import json, pickle

with open("data.pickle", "rb") as fpick:
    with open("data.json", "w") as fjson:
        json.dump(pickle.load(fpick), fjson)

Solution 2

Quote from the documentation:

The data format used by pickle is Python-specific. This has the advantage that there are no restrictions imposed by external standards such as XDR (which can’t represent pointer sharing); however it means that non-Python programs may not be able to reconstruct pickled Python objects.

So the answer to your question is no, you cannot deserialize it in C#. You will have to use an interoperable format such as XML or JSON if you need to communicate with other platforms.

Solution 3

You can try embedding IronPython and unpickling from there, then making the unpickled object available to the C# application.

Note that pickles are designed to serialize Python objects, so this approach only works if you have very simple objects with clear mappings to C# equivalents. It also requires that your IronPython environment have access to all modules defining the classes of all objects contained in the pickle (same as in CPython).

You should try to serialize your data some other more interoperable way (such as JSON or XML) if possible.

Solution 4

Pyrolite has an Unpickler class that will turn a pickle into an object.

Share:
11,504
Nyan Cat
Author by

Nyan Cat

Updated on July 24, 2022

Comments

  • Nyan Cat
    Nyan Cat almost 2 years

    I have some python data, serialized to pickles and need to use it in C# program. So is there any way to deserialize python pickles in C#? I can't change data format to JSON or etc.

  • BenDundee
    BenDundee over 11 years
    We run Python scripts from C# where I work. I can't remember the exact assembly name you need (Microsoft.Scripting i think).
  • anitasp
    anitasp over 5 years
    My pickle file contains a model from sklearn. I got this error: TypeError: Object of type 'LinearSVC' is not JSON serializable. Is there a way to serialize it?