Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I read and write with msgpack?

Tags:

python

msgpack

How do I serialize / deserialize a dictionary data with msgpack?

like image 679
Martin Thoma Avatar asked Apr 16 '17 21:04

Martin Thoma


1 Answers

The Python docs seem not to be so good, so here is my try.

Installation

pip install msgpack

Read and Write msgpack

#!/usr/bin/env python
# -*- coding: utf-8 -*-
import msgpack

# Define data
data = {
    "a list": [1, 42, 3.141, 1337, "help"],
    "a string": "bla",
    "another dict": {"foo": "bar", "key": "value", "the answer": 42},
}

# Write msgpack file
with open("data.msgpack", "wb") as outfile:
    packed = msgpack.packb(data)
    outfile.write(packed)

# Read msgpack file
with open("data.msgpack", "rb") as data_file:
    byte_data = data_file.read()

data_loaded = msgpack.unpackb(byte_data)
print(data == data_loaded)

Alternatives

  • CSV: Super simple format (read & write)
  • JSON: Nice for writing human-readable data; VERY commonly used (read & write)
  • YAML: YAML is a superset of JSON, but easier to read (read & write, comparison of JSON and YAML)
  • pickle: A Python serialization format (read & write)
  • MessagePack (Python package): More compact representation (read & write)
  • HDF5 (Python package): Nice for matrices (read & write)
  • XML: exists too *sigh* (read & write)

For your application, the following might be important:

  • Support by other programming languages
  • Reading / writing performance
  • Compactness (file size)

See also: Comparison of data serialization formats

In case you are rather looking for a way to make configuration files, you might want to read my short article Configuration files in Python

like image 58
Martin Thoma Avatar answered Oct 11 '22 23:10

Martin Thoma