You can read your data as a text file instead of binary file.

Here's how:

file = open("/dev/rtlightsensor0", "r")
line = file.readline()
data = [int(each) for each in line.split())

Original answer:

You didn't describe your question clearly enough, but I'm guessing that you have four 32-bit (4 bytes) int in your file, which you want to read into your program.

os.read is a binary file read function, so the variable data you got is bytes instead of a list of numbers.

Python can't infer the length your number as it can be four 32-bit numbers, one 128-bit number, or something else. You need to define it explicitly.

There is also the issue of whether your data are big endian or little endian.

If your data doesn't come from a network communication, it's likely to be little endian. If you find the result from the following code is wrong (for example, unbelievably large), you can try to convert 'big' into 'little'.

You can finish your code like this:

dev = os.open("/dev/rtlightsensor0", os.0_RDWR)
raw_data = os.read(dev,16)
data = []
data.append(int.from_bytes(raw_data[0:4], 'little'))
data.append(int.from_bytes(raw_data[4:8], 'little'))
data.append(int.from_bytes(raw_data[8:12], 'little'))
data.append(int.from_bytes(raw_data[12:16], 'little'))
print(data)

However, DO NOT do it like that because it's really silly. If you can understand the code above, then you can write it better like this:

dev = os.open("/dev/rtlightsensor0", os.0_RDWR)
raw_data = os.read(dev, 16)
data = [int.from_bytes(raw_data[i * 4: (i + 1) * 4], 'little') for i in range(4)]
print(data)

By the way, it's not wise to read binary data like this. Generally speaking, the package numpy is recommended.

Answer from HolderRoberts on Stack Overflow
🌐
Python
docs.python.org › 3 › c-api › buffer.html
Buffer Protocol — Python 3.14.8 documentation
Certain objects available in Python wrap access to an underlying memory array or buffer. Such objects include the built-in bytes and bytearray, and some extension types like array.array. Third-part...
Top answer
1 of 3
2

You can read your data as a text file instead of binary file.

Here's how:

file = open("/dev/rtlightsensor0", "r")
line = file.readline()
data = [int(each) for each in line.split())

Original answer:

You didn't describe your question clearly enough, but I'm guessing that you have four 32-bit (4 bytes) int in your file, which you want to read into your program.

os.read is a binary file read function, so the variable data you got is bytes instead of a list of numbers.

Python can't infer the length your number as it can be four 32-bit numbers, one 128-bit number, or something else. You need to define it explicitly.

There is also the issue of whether your data are big endian or little endian.

If your data doesn't come from a network communication, it's likely to be little endian. If you find the result from the following code is wrong (for example, unbelievably large), you can try to convert 'big' into 'little'.

You can finish your code like this:

dev = os.open("/dev/rtlightsensor0", os.0_RDWR)
raw_data = os.read(dev,16)
data = []
data.append(int.from_bytes(raw_data[0:4], 'little'))
data.append(int.from_bytes(raw_data[4:8], 'little'))
data.append(int.from_bytes(raw_data[8:12], 'little'))
data.append(int.from_bytes(raw_data[12:16], 'little'))
print(data)

However, DO NOT do it like that because it's really silly. If you can understand the code above, then you can write it better like this:

dev = os.open("/dev/rtlightsensor0", os.0_RDWR)
raw_data = os.read(dev, 16)
data = [int.from_bytes(raw_data[i * 4: (i + 1) * 4], 'little') for i in range(4)]
print(data)

By the way, it's not wise to read binary data like this. Generally speaking, the package numpy is recommended.

2 of 3
1

It is a byte array. You can convert the byte array to string using decode function

>>> test = b'1 53 -5 1\n'
>>> type(test)
<class 'bytes'>
>>> test[0]
49
>>> test_1 = test.decode("utf-8")
>>> test_1[0]
'1'
Discussions

numpy - Python: How to deal with expected a readable buffer object in Python - Stack Overflow
I am quitenew to python and I am working on a python script I inherited from someone I work with, and I am getting this error: expected a readable buffer object The code causing this is: self. More on stackoverflow.com
🌐 stackoverflow.com
Buffer protocol types
We had several typeshed issues and pull requests lately that try to work around the fact that there is no way to express that a method receives any object following the buffer protocol. The typing ... More on github.com
🌐 github.com
21
November 20, 2018
Reading data from a binary buffer
I have a bytes buffer called data and need to read its content. It was created by a C++ program and sent over the network. Its layout is known, but a has a variable length. Here is an example: print(data) id = int.from_… More on discuss.python.org
🌐 discuss.python.org
7
0
May 24, 2024
What is the Python 'buffer' type for? - Stack Overflow
Communities for your favorite technologies. Explore all Collectives · Turn scattered docs and code into trusted AI context. Explore the new Stack Internal platform More on stackoverflow.com
🌐 stackoverflow.com
🌐
GitHub
github.com › python › typeshed › issues › 7601
Accept `ReadableBuffer` instead of `bytes` in `base64` encode functions? · Issue #7601 · python/typeshed
April 6, 2022 - Right now, all of the encode methods in base64 accept bytes but the docs say that they should (and do) accept any bytes-like object. typeshed/stdlib/base64.pyi Line 49 in 536f783 def b64encode(s: bytes, altchars: bytes | None = ...) -> b...
Author: python
🌐
CircuitPython
docs.circuitpython.org › projects › adafruit-circuitpython-typing › en › latest › api.html
circuitpython_typing — Adafruit CircuitPython circuitpython_typing Library 1.0 documentation
circuitpython_typing.ReadableBuffer · Classes that implement the readable buffer protocol. alias of array | bytearray | bytes | memoryview | rgbmatrix.RGBMatrix | ulab.numpy.ndarray · circuitpython_typing.WriteableBuffer · Classes that implement the writeable buffer protocol.
🌐
Python Morsels
pythonmorsels.com › how-does-file-buffering-work
How does file buffering work? - Python Morsels
October 28, 2022 - Instead, Python stores a buffer of bytes in the BufferedReader object that our text file wraps around, and then our text file keeps track of where it left off within that buffer.
🌐
Python
docs.python.org › 3.11 › c-api › objbuffer.html
Old Buffer Protocol — Python 3.11.16 documentation
These functions were part of the “old buffer protocol” API in Python 2. In Python 3, this protocol doesn’t exist anymore but the functions are still exposed to ease porting 2.x code. They act as a ...
Top answer
1 of 2
5

It's hard to respond without knowing what the enums.DataPoints dtype object looks like, but I'll try to explain where you see that error message.

When you try to set (an element of) an array to some value that doesn't align properly with its dtype, you will see this. Here is an example:

In [133]: data = np.zeros((3,2), dtype="int, int")

In [134]: data
Out[134]: 
array([[(0, 0), (0, 0)],
       [(0, 0), (0, 0)],
       [(0, 0), (0, 0)]], 
      dtype=[('f0', '<i8'), ('f1', '<i8')])

In [135]: data[0, 0]
Out[135]: (0, 0)

In [136]: data[0, 0] = [1,2]
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-136-399e78675be4> in <module>()
----> 1 data[0, 0] = [1,2]

TypeError: expected a readable buffer object

This gave an error because it can't handle two values being assigned to one element of your array. Your dtype has two values in it, so it seems reasonable to expect, but the array wants one object of type given by the dtype:

In [137]: data[0,0] = (1,2)

In [138]: data
Out[138]: 
array([[(1, 2), (0, 0)],
       [(0, 0), (0, 0)],
       [(0, 0), (0, 0)]], 
      dtype=[('f0', '<i8'), ('f1', '<i8')])

It's likely that a single set of zeros of the same shape of your array won't align with the dtype.

2 of 2
1

The answer to this was that I was not working under the same version of Numpy that my colleagues were using, as pointed out by @askewchan in the comments to his answer.

  • Failed: numpy 1.6.1
  • Works: numpy 1.7
  • Works: numpy 1.8.0
Find elsewhere
🌐
GitHub
github.com › python › typing › issues › 593
Buffer protocol types · Issue #593 · python/typing
November 20, 2018 - We had several typeshed issues and pull requests lately that try to work around the fact that there is no way to express that a method receives any object following the buffer protocol. The typing documentation mentions BytesType ByteStr...
Author: python
🌐
Python Data Science Handbook
jakevdp.github.io › blog › 2014 › 05 › 05 › introduction-to-the-python-buffer-protocol
An Introduction to the Python Buffer Protocol | Pythonic Perambulations
The Python buffer protocol, also known in the community as PEP 3118, is a framework in which Python objects can expose raw byte arrays to other Python objects. This can be extremely useful for scientific computing, where we often use packages such as NumPy to efficiently store and manipulate ...
🌐
Adafruit
learn.adafruit.com › creating-and-sharing-a-circuitpython-library › typing-information
Typing Information | Creating and sharing a CircuitPython library | Adafruit Learning System
July 31, 2017 - try: # imports used only for typing from typing import Tuple # <- this line causes the error # any imports below this won't happen if the error gets raised from circuitpython_typing import ReadableBuffer from busio import I2C except ImportError: pass # ignore the error
🌐
GitHub
github.com › python › typeshed › blob › main › stdlib › _codecs.pyi
typeshed/stdlib/_codecs.pyi at main · python/typeshed
def decode(obj: ReadableBuffer, encoding: _BytesToBytesEncoding, errors: str = "strict") -> bytes: ...
Author: python
🌐
Python.org
discuss.python.org › python help
Reading data from a binary buffer - Python Help - Discussions on Python.org
May 24, 2024 - I have a bytes buffer called data and need to read its content. It was created by a C++ program and sent over the network. Its layout is known, but a has a variable length. Here is an example: print(data) id = int.from_…
🌐
Medium
medium.com › @AlexanderObregon › how-pythons-memoryview-and-buffers-work-23c489fbbabb
How Python’s Memoryview and Buffers Work | Medium
August 11, 2024 - In Python, the buffer protocol is implemented through a set of C-level interfaces that enable one object to expose its data buffer to another. Objects that support the buffer protocol include built-in types like bytes, bytearray, and third-party ...
🌐
Delft Stack
delftstack.com › home › howto › python › buffer interface in python
Buffer in Python | Delft Stack
March 11, 2025 - This interface defines a set of methods that allow you to access the raw bytes of an object, making it easier to work with binary data. By utilizing the buffer interface, you can read and write data more efficiently, leveraging Python’s ...
🌐
DaniWeb
daniweb.com › programming › software-development › threads › 184421 › typeerror-expected-a-readable-buffer-object
python - TypeError: expected a readable buffer object [SOLVED] | DaniWeb
March 31, 2009 - The underlying issue is that a 2D float array cannot be reinterpreted as a structured dtype with field names via asarray(..., dtype=structured) unless the memory layout exactly matches what NumPy expects for that struct. When it does not, old NumPy versions tended to fail with messages like "expected a readable buffer object." Your list-of-tuples workaround works, but it does all the packing in Python, which can be slow for big arrays.
🌐
Finxter
blog.finxter.com › home › learn python blog › converting python bytes to a bufferedreader
Converting Python Bytes to a BufferedReader - Be on the Right Side of Change
February 23, 2024 - This method writes the bytes to a temporary file and immediately wraps it with a BufferedReader. The temporary file is automatically handled and cleaned up by Python.