Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Preferred (or most common) file extension for a Python pickle

At times, I've seen .pickle, .pck, .pcl, and .db for files that contain Python pickles, but I am unsure what is the most common or best practice. I know that the latter three extensions are also used for other things.

The related question is: What MIME type is preferred for sending pickles between systems using a REST API?

like image 722
Raymond Hettinger Avatar asked Nov 05 '16 00:11

Raymond Hettinger


People also ask

Which pickle method is used to write a Python object to a file?

dump(obj) – This function is used to write a pickled representation of obj to the open file object given in the constructor.

Is pickle file a binary file?

Python pickle serializeThe data is pickled into a binary file. The dump function writes the pickled representation of the object to the file object.

Is JSON or python pickle faster?

JSON is a lightweight format and is much faster than Pickling.

Which method is used to pickle a file?

Pickling is alternatively known as "serialization", "marshalling", or "flattening". To serialize (pickle) an object hierarchy, you simply call the dump() function. Similarly, to de-serialize a data stream, you call the load() function. You can pickle any object like integers, strings, tuples, lists, dictionaries, etc.


1 Answers

Python 2

From the Python 2 documentation, while serializing (i.e. writing to a pickle file), use:

output = open('data.pkl', 'wb') 

I would choose .pkl as the extension when using Python 2.

Python 3

The example in the Python 3 documentation now uses .pickle as the file extension for serialization:

with open('data.pickle', 'wb') as f:     pickle.dump(...) 

The MIME type preferred for sending pickles from martineau's comment below:

application/octet-stream

See What is the HTTP "content-type" to use for a blob of bytes?

like image 55
TheoretiCAL Avatar answered Sep 23 '22 21:09

TheoretiCAL