'How to convert a string variable to an Array in Python?

I have a string value like this: {"id": "312749", "315082", "316379", "316648", "320454", "321766"} I would like it to be read as an array in python. Ids as the table name and the ids values as variables of the array. I'm learning python I guess I must not be using the right terms for my search, I didn't find the solution. Could you help me?



Solution 1:[1]

Assuming that the OP really means a list rather than a [numpy] array, this would work:

import re
from numpy import array
STRING = '{"ids": 319242, 322456, 327484"}'
LIST = [int(n) for n in re.findall('\d+', STRING)]
print(LIST)
ARRAY = array(LIST)
print(ARRAY)

Output:

[319242, 322456, 327484]
[319242 322456 327484]

EDIT:

Added numpy array

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1