Reputation: 791
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!
Upvotes: 5
Views: 6832
Reputation: 231325
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')
Upvotes: 4
Reputation: 249093
numpy.fromstring()
is useful for reading numbers, but for tokenizing strings you can do this:
numpy.core.defchararray.split(data, sep=",")
Upvotes: 2