简体   繁体   中英

What are the basic difference between pickle and yaml in Python?

I am naive to Python. But, what I came to know is that both are being used for serialization and deserialization. So, I just want to know what all basic differences in between them?

YAML is a language-neutral format that can represent primitive types (int, string, etc.) well, and is highly portable between languages. Kind of analogous to JSON, XML or a plain-text file; just with some useful formatting conventions mixed in -- in fact, YAML is a superset of JSON.

Pickle format is specific to Python and can represent a wide variety of data structures and objects, eg Python lists, sets and dictionaries; instances of Python classes; and combinations of these like lists of objects; objects containing dicts containing lists; etc.

So basically:

  • YAML represents simple data types & structures in a language-portable manner
  • pickle can represent complex structures, but in a non-language-portable manner

There's more to it than that, but you asked for the "basic" difference.

pickle is a special python serialization format when a python object is converted into a byte stream and back:

“Pickling” is the process whereby a Python object hierarchy is converted into a byte stream, and “unpickling” is the inverse operation, whereby a byte stream is converted back into an object hierarchy.

The main point is that it is python specific.

On the other hand, YAML is language-agnostic and human-readable serialization format.

FYI, if you are choosing between these formats, think about:

  • serialization/derialization speed (see cPickle module)
  • do you need to store serialized files in a human-readable form?
  • what are you going to serialize? If it's a python-specific complex data structure, for example, then you should go with pickle.

See also:

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM