Reputation: 67
I use this syntax to convert the byte array dataword (2 bytes for each sample):
data = numpy.fromstring(dataword, dtype=numpy.int16)
same instruction in Python 3.7 returns the error:
TypeError: fromstring() argument 1 must be read-only bytes-like object, not memoryview
dataword = scope.ReadBinary(rlen-4) #dataword is a byte array, each 2 byte is an integer
data = numpy.fromstring(dataword, dtype=numpy.int16)# data is the int16 array
This is the content of data
in Python 2.7.14:
[-1.41601562 -1.42382812 -1.42578125 ..., 1.66992188 1.65234375 1.671875 ]
I am expecting to get the same result with Python 3.7.
How am I supposed to use numpy.fromstring()
in 3.7?
Upvotes: 1
Views: 1035
Reputation: 67
fromstring doesn't work because dataword is a memeoryview not a string, then frombuffer musty be used
data = numpy.frombuffer(dataword.tobytes(), dtype=numpy.int16)
Upvotes: 0
Reputation: 67
simple solution... found reading numpy manual: replace fromstring with frombuffer
data = numpy.frombuffer(dataword, dtype=numpy.int16) works perfectly
Upvotes: 1
Reputation: 183
The TypeError
tries to tell you that dataword
is of the non-supported type memoryview
.
It needs to be passed as an unmutable type, like bytes
:
data = numpy.fromstring(dataword.tobytes(), dtype=numpy.int16)
Even better; it seems like scope
is a file-like object, so this could also work:
data = numpy.fromfile(scope, dtype=numpy.int16, count=rlen//2-4)
Upvotes: 1