Convert dictionary to bytes and back again python?

PythonVariablesDictionaryByte

Python Problem Overview


I need to send the value of some variables between two machines and intend to do it using sockets. I use the md5 hash algorithm as a checksum for the data I send to ensure the data is correctly transmitted. To perform the md5 hash algorithm I have to convert the data to bytes. I want to transmit both the name of the variable and its value. As I have a lot of variables i use a dictionary.

So I want to convert something like this to bytes?

variables = {'var1' : 0, 'var2' : 'some string', 'var1' : ['listitem1','listitem2',5]}

In other words I have a dictionary with a lot of different data types inside it including lists which in turn have multiple different data types in them and I want to convert that into bytes. Then on the receiving machine convert those bytes back into a dictionary.

I have tried a few different methods json is recomended here (https://stackoverflow.com/questions/4547274/convert-a-python-dict-to-a-string-and-back) but I can't seam to produce a string with it never mind bytes.

Python Solutions


Solution 1 - Python

This should work:

import json

s = json.dumps(variables)
variables2 = json.loads(s)
assert variables == variables2

Solution 2 - Python

If you need to convert the dictionary to binary, you need to convert it to a string (JSON) as described in the previous answer, then you can convert it to binary.

For example:

my_dict = {'key' : [1,2,3]}

import json
def dict_to_binary(the_dict):
    str = json.dumps(the_dict)
    binary = ' '.join(format(ord(letter), 'b') for letter in str)
    return binary


def binary_to_dict(the_binary):
    jsn = ''.join(chr(int(x, 2)) for x in the_binary.split())
    d = json.loads(jsn)  
    return d

bin = dict_to_binary(my_dict)
print bin

dct = binary_to_dict(bin)
print dct

will give the output

1111011 100010 1101011 100010 111010 100000 1011011 110001 101100 100000 110010 101100 100000 110011 1011101 1111101

{u'key': [1, 2, 3]}

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionuser1205406View Question on Stackoverflow
Solution 1 - PythonRobᵩView Answer on Stackoverflow
Solution 2 - PythonshshankView Answer on Stackoverflow