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.
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.
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'
python - Reading contents of buffer line by line - Stack Overflow
input - Python read from buffer - Stack Overflow
Buffer/stream to read a subset of a file in Python? - Stack Overflow
Get buffer size
I have a BytesIO buffer (BufferedReader) I need a way to read it's current data, I can't just use read() because the read function is waiting for an EOF char, but because the buffer is not complete yet it doesnt have that char.
I need a way to read all the data that the buffer has as this moment. Maybe get the length of the data in there, etc.