我有一个像这样的bytes类型的对象。
我需要使用Python代码从上述字节类型对象中获取正确的Python字典。
但是我遇到了以下错误:
b"{'one': 1, 'two': 2}"
我需要使用Python代码从上述字节类型对象中获取正确的Python字典。
string = b"{'one': 1, 'two': 2}"
d = dict(toks.split(":") for toks in string.split(",") if toks)
但是我遇到了以下错误:
------> d = dict(toks.split(":") for toks in string.split(",") if toks)
TypeError: 'bytes' object is not callable
ast.literal_eval
。 - Patrick Haugh" 'two'"
而不是"two"
的键)。 - abarnertrepr
转换为 UTF-8 编码真的不是一种好的存储数据以便稍后加载的方式。更好的方法是使用类似 JSON 或 Pickle 的东西。 - abarnert