如何将字节字典解码为utf-8?

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

我试图弄清楚如何将字典的值从字节转换为字符串,因为后端仅支持基元类型。

 oledata = {
            'macros': macros,
            'data': analysis
        }
        s = str(oledata)
        save_data_to_s3(json.dumps(s), ['olevba3'])

如您所见,此dict的值是字节。现在这段代码在我的测试样本上执行时没有错误,但是输出在值(数据)前面有b'前缀,这将破坏数据库。 Dict也没有decode()功能,这就是我使用str()的原因,但是它必须做错了,因为值仍然以b'前缀出现。这导致了我的一般性问题,如何将字典的值解码为utf-8格式?

python-3.x python-2.7
1个回答
0
投票
my_str = b"Hello" # b means its a byte string
new_str = my_str.decode('utf-8') # Decode using the utf-8 encoding
print(new_str)
© www.soinside.com 2019 - 2024. All rights reserved.