5

I have a string of data data = "1,Hey,234,4456,789" that I want to convert into a numpy array. Whenever I try the code numpy.fromstring(data,dtype=str,sep=","), I get the error "ValueError: zero-valued itemsize". What is the correct way to use this function so that it works as intended? The output I am trying to get is np.array(['1','Hey','234','4456','789']). Thanks!

2 Answers 2

4

Just turn the string into a list of strings (with split) and give that to array.

In [21]: np.array("1,Hey,234,4456,789".split(',')) Out[21]: array(['1', 'Hey', '234', '4456', '789'], dtype='|S4') 
Sign up to request clarification or add additional context in comments.

Comments

2

numpy.fromstring() is useful for reading numbers, but for tokenizing strings you can do this:

numpy.core.defchararray.split(data, sep=",") 

1 Comment

This produces a numpy array with a list inside of it.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.