Join the Stack Overflow Community
Stack Overflow is a community of 6.6 million programmers, just like you, helping each other.
Join them; it only takes a minute:
Sign up

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!

share|improve this question
up vote 1 down vote accepted

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')
share|improve this answer
    
I did that and it worked! Thanks – Lahav Dec 5 '15 at 5:02

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

numpy.core.defchararray.split(data, sep=",")
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.