Python list string to list

后端 未结 6 1388
佛祖请我去吃肉
佛祖请我去吃肉 2021-01-21 11:51

I have a string:

s= \"[7, 9, 41, [32, 67]]\"

and I need to convert that string into a list:

l= [7, 9, 41, [32, 67]]
         


        
6条回答
  •  说谎
    说谎 (楼主)
    2021-01-21 12:38

    It is another answer, But I don't suggest you.Because exec is dangerous.

    >>> s= "[7, 9, 41, [32, 67]]"
    >>> try:
    ...   exec 'l = ' + s
    ...   l
    ... except Exception as e:
    ...   e
    [7, 9, 41, [32, 67]]
    

提交回复
热议问题