如何在python 3中将字节文字列表转换为字符串文字?

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

我有一个字节文字列表:

list1 = [b'R103', b'R102', b'R109', b'R103']

我想将此字节文字列表转换为字符串文字。像这样:

list1 = ['R103', 'R102', 'R109', 'R103']

我尝试使用解码:

list1.decode("UTF-8")

但是,解码不适用于列表。我最终收到以下错误消息:

AttributeError: 'list' object has no attribute 'decode'

是否有一种方法可以将整个列表转换为我缺少的字符串文字?

python string byte decode literals
1个回答
0
投票
list2 = [x.decode("UTF-8") for x in list1]
In [1]: list1 = [b'R103', b'R102', b'R109', b'R103']

In [2]: list2 = [x.decode("UTF-8") for x in list1]

In [3]: list2
Out[3]: ['R103', 'R102', 'R109', 'R103']
© www.soinside.com 2019 - 2024. All rights reserved.