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

问题描述 投票:0回答:1

我有以下词典: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'。我该怎么办?

谢谢-埃文

python dictionary encoding byte decoding
1个回答
0
投票

这里是将解码后的项目存储到另一本词典中的方法:

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

decoded = {}

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

print(decoded)

输出:

{'335139450613137430': '1', '704168692828864574': '22'}
© www.soinside.com 2019 - 2024. All rights reserved.