2015-05-28 54 views
2

我有這個字符串:CONVER名單,以列表的列表的列表的字符串

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']] 

我如何得到的這份名單從那個巨大的字符串列表?

回答

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

您可以使用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)) 
相關問題