Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to encode python dictionary?

I want to encode the sample stuff shown below:

name = "Myname"
status = "married"
sex = "Male"
color = {'eyeColor' : 'brown', 'hairColor' : 'golden', 'skinColor' : 'white'}

I am using base64 encoding scheme and used syntax as <field-name>.encode('base64','strict') where field-name consists of above mentioned fields- name, status and so on.

Everything except dictionary "color" is getting encoded. I get error at color.encode('base64','strict')

The error is as shown below:

Traceback (most recent call last):
    color.encode('base64','strict') 
AttributeError: 'CaseInsensitiveDict' object has no attribute 'encode'

I think encode method is not appicable on dictionary. How shall I encode the complete dictionary at once? Is there any alternative to encode method which is applicable on dictionaries?

like image 862
v1h5 Avatar asked Jul 01 '14 11:07

v1h5


People also ask

Can you encode a dictionary in python?

Python has a module to do that, it's called pickle. Pickle can help you get a string representation of any object, which you can then encode to base64. After you decode it back, you can also unpickle it to get back the original instance. Other alternatives to pickle + base64 might be json.

How do you encode a string in a dictionary python?

Use the str() and the literal_eval() Function From the ast Library to Convert a Dictionary to a String and Back in Python. This method can be used if the dictionary's length is not too big. The str() method of Python is used to convert a dictionary to its string representation.

How do you write a dictionary in python?

Creating Python Dictionary Creating a dictionary is as simple as placing items inside curly braces {} separated by commas. An item has a key and a corresponding value that is expressed as a pair (key: value).

What is meant by dictionary encoding?

Dictionary encoding is a data compression technique that can be applied to individual columns of the following effective types: int. long. date.


3 Answers

encode is a method that string instances has, not dictionaries. You can't simply use it with every instance of every object. So the simplest solution would be to call str on the dictionary first:

str(color).encode('base64','strict')

However, this is less straight forward when you'd want to decode your string and get that dictionary back. Python has a module to do that, it's called pickle. Pickle can help you get a string representation of any object, which you can then encode to base64. After you decode it back, you can also unpickle it to get back the original instance.

b64_color = pickle.dumps(color).encode('base64', 'strict')
color = pickle.loads(b64_color.decode('base64', 'strict'))

Other alternatives to pickle + base64 might be json.

like image 178
Korem Avatar answered Oct 27 '22 02:10

Korem


color = {'eyeColor' : 'brown', 'hairColor' : 'golden', 'skinColor' : 'white'}
base64.urlsafe_b64encode(json.dumps(color).encode()).decode()
like image 4
Banu Soppan Avatar answered Oct 27 '22 02:10

Banu Soppan


simple and easy way:

import json
converted_color = json.dumps(color)
encoded_color = converted_tuple.encode()
print(encoded_tuple)
decoded_color = encoded_color.decode()
orginal_form = json.load(decoded_color)
like image 1
Ani Avatar answered Oct 27 '22 00:10

Ani