Physicist
Physicist

Reputation: 3048

splitting string / bytes in Python 3

I downloaded some code from Github for interfacing with a lab equipment:

readBuffer=ctypes.create_string_buffer(1024)
# some C functions modifying readBuffer using ctypes
deviceInfoList=readBuffer.value.split(',')

The last line shows an error: TypeError: a bytes-like object is required, not 'str'

print(type(readBuffer.value)) shows <class 'bytes'>, and print(readBuffer.value) shows b'0,NEWPORT 1936-R v1.2.2 04/06/12 SN22955\r;'.

According to the SDK documentation file, readBuffer is 'a character buffer with the following format: <DeviceID1>,<DeviceDescription1>;<DeviceID2>,<DeviceDescription2>;<DeviceIDX>,<DeviceDescriptionX>. The last line of the code wants to extract the device ID (which is 0).

Split should be a string method if I'm not mistaken, readBuffer.value looks like a byte, while the error complains that it is string not byte. What has gone wrong here?

Upvotes: 1

Views: 825

Answers (1)

kabanus
kabanus

Reputation: 25895

Both string and bytes have a split method, that requires an argument of the same type. ',' is not a bytes object - hence the complaint. You want

deviceInfoList=readBuffer.value.split(b',')

Upvotes: 2

Related Questions