9

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.

5 Answers 5

10

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)
Sign up to request clarification or add additional context in comments.

1 Comment

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?
4

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.

Comments

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.

1 Comment

We run Python scripts from C# where I work. I can't remember the exact assembly name you need (Microsoft.Scripting i think).
3

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

Comments

1

There is now a NuGet Razorvine.Pickle, for serializing and deserializing pickle files in .NET.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.