我有这个字符串:
num="['1', '9', '7', '6'],['2', '0', '8', '3', '7'],['3', '8', '5', '7', '9', '10', '4']"
我希望返回/输出:
[['1', '9', '7', '6'],['2', '0', '8', '3', '7'],['3', '8', '5', '7', '9', '10', '4']]
如何从这个庞大的字符串中获取这个列表列表?
答案 0 :(得分:4)
ast.literal_eval
正是如此。
>>> num="['1', '9', '7', '6'],['2', '0', '8', '3', '7'],['3', '8', '5', '7', '9', '10', '4']"
>>> import ast
>>> list(ast.literal_eval(num))
[['1', '9', '7', '6'], ['2', '0', '8', '3', '7'], ['3', '8', '5', '7', '9', '10', '4']]
答案 1 :(得分:1)
您可以使用AST:
import ast
num="['1', '9', '7', '6'],['2', '0', '8', '3', '7'],['3', '8', '5', '7', '9', '10', '4']"
num = list(ast.literal_eval(num))