如何在Python中解析字符串列表?

时间:2017-04-12 15:07:11

标签: python python-2.7

如何在Python中解析字符串列表?

myStringValue = "[['5', 1], ['1', 5], ['3', 3], ['3', 6], ['4', 5], ['4', 3], ['4', 5], ['6', 6], ['4', 1], ['3', 4]]"

如何将myStringValue转换为列表?

1 个答案:

答案 0 :(得分:1)

使用它时要小心,因为它可以被利用,但eval()是前进的方向:

>>> myStringValue = "[['5', 1], ['1', 5], ['3', 3], ['3', 6], ['4', 5], ['4', 3], ['4', 5], ['6', 6], ['4', 1], ['3', 4]]"
>>> eval(myStringValue)
[['5', 1], ['1', 5], ['3', 3], ['3', 6], ['4', 5], ['4', 3], ['4', 5], ['6', 6], ['4', 1], ['3', 4]]
>>> type(eval(myStringValue))
<type 'list'>