Reputation: 2257
I want to pass csv data as argument in postman.
Which can be like
s = 2,3,4,5
s= "2,3,4,5"
This csv data is coming from some csv file. I can directly pas it like
localhost?data="2,3,4,5"
How to parse it correctly and convert it into numpy array?
I tried this
s = "2,3,4,5"
print(np.array(list(s)))
Which gives
['1' ',' '2' ',' '3' ',' '4']
which is wrong.
d =np.fromstring(s[1:-1],sep=' ').astype(int)
Gives array([], dtype=int64)
which I dont understand.
What is the correct way?
Upvotes: 1
Views: 109
Reputation: 82795
You can split on comma and then use np.array
Ex:
import numpy as np
s = "2,3,4,5"
print(np.array(s.strip('"').split(",")).astype(int))
Output:
[2 3 4 5]
Upvotes: 2
Reputation: 5207
You could try np.fromstring()
as in
import numpy as np
s = "2,3,4,5"
np.fromstring(s, dtype=int, sep=',')
to get output like
array([2, 3, 4, 5])
Upvotes: 4
Reputation: 5372
Here is another way:
>>> import numpy as np
>>> s='2,3,4,5'
>>> np.array([int(i) for i in s.split(',')])
array([2, 3, 4, 5])
>>>
Upvotes: 1