Sorry for the possible repeats of the question. I have a weird string, for example:
'[23, 14, 34]'
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:
[23, 14, 34]
Any insights? Thanks in advance.
Advertisement
Answer
Using json.loads()
import json ini_list = "[1, 2, 3, 4, 5]" print ("initial string", ini_list) print (type(ini_list)) res = json.loads(ini_list) print ("final list", res) print (type(res))