[ACCEPTED]-How do I create a numpy array from string?-fft
Accepted answer
In Python 2, you can do this directly with 6 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 5 use the regular scipy/numpy FFT routines.
In 4 Python 3, the switch to default Unicode 3 strings means that you would read in the 2 data as a bytestring and use the frombuffer
command 1 instead:
import numpy as np
s = b'\x01\x05\x03\xff'
a = np.frombuffer(s, dtype='uint8')
to get the same results.
>>> '\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])
0
Without knowing what you've got coming in 2 it's tough, but if it were comma delimited 1 integers you could do something like this:
myInts = map(int, myString.split(','))
Source:
stackoverflow.com
More Related questions
Cookie Warning
We use cookies to improve the performance of the site. By staying on our site, you agree to the terms of use of cookies.