Sorry for the possible repeats of the question. I have a weird string, for example:
JavaScript
x
2
1
'[23, 14, 34]'
2
ps: there is a comma “,” and a space ” ” separating each number. The whole thing is recognized as string. I want to convert it into a list of integers:
JavaScript
1
2
1
[23, 14, 34]
2
Any insights? Thanks in advance.
Advertisement
Answer
Using json.loads()
JavaScript
1
9
1
import json
2
3
ini_list = "[1, 2, 3, 4, 5]"
4
print ("initial string", ini_list)
5
print (type(ini_list))
6
res = json.loads(ini_list)
7
print ("final list", res)
8
print (type(res))
9