Possible Duplicate:
Converting a string that represents a list, into an actual list object
Having a string like this:
"[81, 102, 114, 132, 194, 210, 228, 317, 357, 358, 379, 396, 407, 417, 418, 420, 470, 471, 506, 526, 533, 538]"
How can I parse it easily into the corresponding list?
I know I could use the re module or splitting by ", ", etc.
Is there any already existing function to do it?
Use ast.literal_eval():
>>> import ast
>>> s = "[81, 102, 114, 132, 194, 210, 228, 317, 357, 358, 379, 396, 407, 417, 418, 420, 470, 471, 506, 526, 533, 538]"
>>> ast.literal_eval(s)
[81, 102, 114, 132, 194, 210, 228, 317, 357, 358, 379, 396, 407, 417, 418, 420, 470, 471, 506, 526, 533, 538]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With