How I can deserialize python pickles in C#?

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)

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.

Tags:

Python

C#

Pickle