将作为字节的列表转换为整数列表

时间:2017-05-29 16:51:46

标签: python-3.x

我试图将字节列表转换为int列表,我拉的数据类似于:

test = b'[[9126, 0.2812168002128601], [9514, 0.2675456404685974], [9342, 0.26063060760498047], [8999, 0.23802196979522705], [9056, 0.23092836141586304], [9053, 0.22339123487472534], [9019, 0.2215365171432495], [9225, 0.21875709295272827]]

当我运行str(test, "utf-8")test.decode("utf-8")时,它会将test转换为长字符串。我需要将test转换为列表,以便我可以遍历它,其中

test[0] = [9126, 0.2812168002128601]
test[1] = [9514, 0.2675456404685974]
...

现在我得到了:

test[0] = [
test[1] = [
test[2] = 9
...

1 个答案:

答案 0 :(得分:0)

>>> import json
>>> test = b'[[9126, 0.2812168002128601], [9514, 0.2675456404685974], [9342, 0.26063060760498047], [8999, 0.23802196979522705], [9056, 0.23092836141586304], [9053, 0.22339123487472534], [9019, 0.2215365171432495], [9225, 0.21875709295272827]]'
>>> new_list = json.loads(test)
>>> new_list
[[9126, 0.2812168002128601], [9514, 0.2675456404685974], [9342, 0.26063060760498047], [8999, 0.23802196979522705], [9056, 0.23092836141586304], [9053, 0.22339123487472534], [9019, 0.2215365171432495], [9225, 0.21875709295272827]]
>>> new_list[0]
[9126, 0.2812168002128601]
>>> new_list[1]
[9514, 0.2675456404685974]