我如何将以字节编码的 python 字典转换为 UTF-8 字典

How could I convert a python dictionary that is encoded in bytes, into a UTF-8 Dictionary

我有以下字典:pointsdict = {b'335139450613137430': b'1', b'704168692828864574': b'22'} 其中第一个元素是用户 ID,第二个元素是点数。怎么修改335139450613137430的积分数

我试过了

decoded = pointsdict.decode()
decoded[userid] = pointstoset
pointsdict = decoded.encode()

并在第一行收到 AttributeError: 'dict' object has no attribute 'decode'。我该怎么做?

谢谢 - 埃文

以下是将解码后的项目存储到另一个字典中的方法:

pointsdict = {b'335139450613137430': b'1', b'704168692828864574': b'22'}

decoded = {}

for key in pointsdict.keys():
    decoded[key.decode('ascii')] = pointsdict[key].decode('ascii')

print(decoded)

输出:

{'335139450613137430': '1', '704168692828864574': '22'}



代码可以像这样更紧凑:

decoded = {key.decode('ascii'):pointsdict[key].decode(ascii) for key in pointsdict.keys()}