简体   繁体   中英

msgpack gets string data back as binary

In How do I read and write with msgpack? an answer ( https://stackoverflow.com/a/43442195 ) is given how to dump data to disk and read it back.

I had to adapt the answer given there (namely: add the "b" option for reading and writing) to get it working. But still the data read from disk is different, as the strings read from disk now seem to be binary.

How can I avoid this?

This is my adapted code:

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:
    msgpack.pack(data, outfile)

# Read msgpack file
with open('data.msgpack', "rb") as data_file:
    # data_loaded = json.load(data_file)
    data_loaded = msgpack.unpack(data_file)

print(data == data_loaded)
print("data:", data)
print("data_loaded:", data_loaded)

The output:

False
data: {'a list': [1, 42, 3.141, 1337, 'help'], 'a string': 'bla', 'another dict': {'foo': 'bar', 'key': 'value', 'the answer': 42}}
data_loaded: {b'a list': [1, 42, 3.141, 1337, b'help'], b'a string': b'bla', b'another dict': {b'foo': b'bar', b'key': b'value', b'the answer': 42}}

The version seems ( msgpack._version.py ) to be:

version = (0, 6, 0)

setting raw=False when reading did the trick. The code:

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:
    msgpack.pack(data, outfile, )

# Read msgpack file
with open('data.msgpack', "rb") as data_file:
    # data_loaded = json.load(data_file)
    data_loaded = msgpack.unpack(data_file,raw=False)

print(data == data_loaded)
print("data:", data)
print("data_loaded:", data_loaded)

the output:

True
data: {'a list': [1, 42, 3.141, 1337, 'help'], 'a string': 'bla', 'another dict': {'foo': 'bar', 'key': 'value', 'the answer': 42}}
data_loaded: {'a list': [1, 42, 3.141, 1337, 'help'], 'a string': 'bla', 'another dict': {'foo': 'bar', 'key': 'value', 'the answer': 42}}

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