1

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?

3
  • What is scope? Commented Mar 27, 2019 at 16:52
  • scope is an activex object, needed the receive the binary vector from an instrument. in python 2.7.14 dataword is defined as a binary array: buffer: ��P��������0����@�p��p����........... Commented Mar 28, 2019 at 12:58
  • python 3.7: in the variable tab of LIclipse i can see that dataword is defined as memoryview:<memory at 0x0f..... definetely not a binary vector as in python 2.17 Commented Mar 28, 2019 at 13:33

3 Answers 3

1

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)
Sign up to request clarification or add additional context in comments.

1 Comment

Understood, all based on the how python3 is handling binary strings. I beleive this will work, going to try and give you an answer. Thanks
1

simple solution... found reading numpy manual: replace fromstring with frombuffer

data = numpy.frombuffer(dataword, dtype=numpy.int16) works perfectly

Comments

0

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)

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.