Convert String to List in Python Without Using Eval?

前端 未结 4 1249
粉色の甜心
粉色の甜心 2020-12-21 05:15

I have a string, something like this: \"[[\'Cheese\', 72], [\'Milk\', 45], [\'Bread\', 22]]\".

I want to convert this to a list. I know I can use eval(s

4条回答
  •  忘掉有多难
    2020-12-21 05:50

    Here's one way: exec("list = "+foo) This should make a variable 'list' with the list in string converted to a real list. This also works for dictionaries, arrays, bools, etc in strings.

提交回复
热议问题