简体   繁体   English

如何编码python字典?

[英]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.我正在使用 base64 编码方案并使用语法作为<field-name>.encode('base64','strict')其中field-name由上述field-name -名称、状态等组成。

Everything except dictionary "color" is getting encoded.除了字典“颜色”之外的所有内容都被编码。 I get error at color.encode('base64','strict')我在color.encode('base64','strict')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方法的替代方法?

encode is a method that string instances has, not dictionaries. encode是字符串实例具有的方法,而不是字典。 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

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 . Python 有一个模块可以做到这一点,它被称为pickle Pickle can help you get a string representation of any object, which you can then encode to base64. Pickle 可以帮助您获得任何对象的字符串表示,然后您可以将其编码为 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 . pickle + base64 的其他替代方案可能是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.这是在 Python 中编码 Python 字典的另一种方式。

I tested in Python 36我在 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'}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM