1

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?

1
  • 1
    print(np.array(s.split(",")).astype(int)) ? Commented Apr 22, 2019 at 11:49

3 Answers 3

4

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])
Sign up to request clarification or add additional context in comments.

Comments

2

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]

2 Comments

Thanks bit it gives me data = np.array(data.split(",")).astype(int) ValueError: invalid literal for int() with base 10: '"1'
use .strip('"')
1

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])
>>> 

2 Comments

Thanks but it gives me data = np.array([int(i) for i in data.split(',')]) ValueError: invalid literal for int() with base 10: '"1'
What data have you used to test it? As you can see in the example above, for the string '2,3,4,5' it is working fine.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.