Sergey Danko
Sergey Danko

Reputation: 18

Converting String to int array

I'm stucked with some problem. I've sent int array from JS to Python via AJAX and it has been converted to JSON (as I used JSON.stringify()), so now it's a string "[1,2,3,4,5]". How can I convert it in Python back to int array [1,2,3,4,5]? I've tried to convert this array to numpy array np.asarray(features_user, dtype="int"), but this not helped

Upvotes: 0

Views: 209

Answers (1)

Lemayzeur
Lemayzeur

Reputation: 8525

json.loads() is what you're looking for. make sure you add the s in load. the s stands for string.

So json.load is for a file, json.loads for a string

>>> import json
>>> a = "[1,2,3,4,5]"
>>> b = json.loads(a)
>>> b,type(b)
([1, 2, 3, 4, 5] , <class 'list'>)

Upvotes: 1

Related Questions