如何将字典列表转换为字节,以便可以对其进行编码和压缩。
obj = [{'State': ['California'], 'location': 'North', 'zipcode': '43567'},
{'State': ['Texas'], 'location': 'Mid', 'zipcode': '43467'}]
base64.b64encode(zlib.compress(str.encode(obj, 'utf-8'), 6))
收到此错误
TypeError: descriptor 'encode' requires a 'str' object but received a 'list'
答案 0 :(得分:2)
您应该首先使用json转储字典列表:
import json
import base64
import zlib
obj = [{'State': ['California'], 'location': 'North', 'zipcode': '43567'}, {'State': ['Texas'], 'location': 'Mid', 'zipcode': '43467'}]
base64.b64encode(zlib.compress(str.encode(json.dumps(obj), 'utf-8'), 6))