简体   繁体   中英

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?

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 .

color = {'eyeColor' : 'brown', 'hairColor' : 'golden', 'skinColor' : 'white'}
base64.urlsafe_b64encode(json.dumps(color).encode()).decode()
# Something like this works on Python 2.7.12
from base64 import b64decode
color = {'eyeColor' : 'brown', 'hairColor' : 'golden', 'skinColor' : 'white'}
encoded_color = str(color).encode('base64','strict')
print(encoded_color)

decoded_color = b64decode(encoded_color)
print(decoded_color)

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)

This is another way to encode python dictionary in Python.

I tested in Python 36

import base64

my_dict = {'name': 'Rajiv Sharma', 'designation': "Technology Supervisor"}
encoded_dict = str(my_dict).encode('utf-8')

base64_dict = base64.b64encode(encoded_dict)
print(base64_dict)

my_dict_again = eval(base64.b64decode(base64_dict))
print(my_dict_again)

Output:

b'eyduYW1lJzogJ1Jhaml2IFNoYXJtYScsICdkZXNpZ25hdGlvbic6ICdUZWNobm9sb2d5IFN1cGVydmlzb3InfQ=='
{'name': 'Rajiv Sharma', 'designation': 'Technology Supervisor'}

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