Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

list.__str__ reversed [duplicate]

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?

like image 384
Flavius Avatar asked Apr 20 '26 17:04

Flavius


1 Answers

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]
like image 75
NPE Avatar answered Apr 23 '26 06:04

NPE



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!