Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How I can deserialize python pickles in C#?

Tags:

python

c#

pickle

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.

like image 994
Nyan Cat Avatar asked Feb 17 '13 15:02

Nyan Cat


People also ask

How do you deserialize a pickle in Python?

The pickle moduleDeserialization is the process of converting a byte stream to Python object. This process is also called pickling/unpickling or marshalling/unmarshalling. The pickletools module contains tools for analyzing data streams generated by pickle. Note: data serialization with the pickle module is insecure.

Is pickling used for Deserialization?

Pickle in Python is primarily used in serializing and deserializing a Python object structure. In other words, it's the process of converting a Python object into a byte stream to store it in a file/database, maintain program state across sessions, or transport data over the network.

How do I Unpickle a pickle file?

You can use the loads() method to unpickle an object that is pickled in the form of a string using the dumps() method, instead of being stored on a disk via the the dump() method. In the following example the car_list object that was pickled to a car_list string is unpickled via the loads() method.

Which method is used for pickling in Python?

Python pickle module is used for serializing and de-serializing python object structures. The process to converts any kind of python objects (list, dict, etc.) into byte streams (0s and 1s) is called pickling or serialization or flattening or marshalling.


1 Answers

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)
like image 135
Ned Batchelder Avatar answered Sep 21 '22 11:09

Ned Batchelder