Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I create a numpy array from string?

I have a file reader that reads n bytes from a file and returns a string of chars representing that (binary) data. I want to read up n bytes into a numpy array of numbers and run a FFT on it, but I'm having trouble creating an array from a string. A couple lines of example would be awesome.

Edit: I'm reading raw binary data, and so the string I get looks like '\x01\x05\x03\xff'.... I want this to become [1, 5, 3, 255].

like image 701
erjiang Avatar asked Nov 03 '10 19:11

erjiang


People also ask

Can I create a NumPy array of strings?

Starting from numpy 1.4, if one needs arrays of strings, it is recommended to use arrays of dtype object_ , string_ or unicode_ , and use the free functions in the numpy. char module for fast vectorized string operations.

How do you convert a string to an array in Python?

The most straightforward way is to type cast the string into a list. Tyepcasting means to directly convert from one data type to another – in this case from the string data type to the list data type. You do this by using the built-in list() function and passing the given string as the argument to the function.

Does NumPy work with string?

The numpy. char module provides a set of vectorized string operations for arrays of type numpy.


3 Answers

In Python 2, you can do this directly with numpy.fromstring:

import numpy as np
s = '\x01\x05\x03\xff'
a = np.fromstring(s, dtype='uint8')

Once completing this, a is array([ 1, 5, 3, 255]) and you can use the regular scipy/numpy FFT routines.

In Python 3, the switch to default Unicode strings means that you would read in the data as a bytestring and use the frombuffer command instead:

import numpy as np
s = b'\x01\x05\x03\xff'
a = np.frombuffer(s, dtype='uint8')

to get the same results.

like image 65
Tim Whitcomb Avatar answered Nov 09 '22 12:11

Tim Whitcomb


>>> '\x01\x05\x03\xff'
'\x01\x05\x03\xff'
>>> map(ord, '\x01\x05\x03\xff')
[1, 5, 3, 255]
>>> numpy.array(map(ord, '\x01\x05\x03\xff'))
array([  1,   5,   3, 255])
like image 26
kennytm Avatar answered Nov 09 '22 14:11

kennytm


Without knowing what you've got coming in it's tough, but if it were comma delimited integers you could do something like this:

myInts = map(int, myString.split(','))
like image 1
g.d.d.c Avatar answered Nov 09 '22 12:11

g.d.d.c