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'
numpy - Python: How to deal with expected a readable buffer object in Python - Stack Overflow
Buffer protocol types
Reading data from a binary buffer
What is the Python 'buffer' type for? - Stack Overflow
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.
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
An example usage:
>>> s = 'Hello world'
>>> t = buffer(s, 6, 5)
>>> t
<read-only buffer for 0x10064a4b0, size 5, offset 6 at 0x100634ab0>
>>> print t
world
The buffer in this case is a sub-string, starting at position 6 with length 5, and it doesn't take extra storage space - it references a slice of the string.
This isn't very useful for short strings like this, but it can be necessary when using large amounts of data. This example uses a mutable bytearray:
>>> s = bytearray(1000000) # a million zeroed bytes
>>> t = buffer(s, 1) # slice cuts off the first byte
>>> s[1] = 5 # set the second element in s
>>> t[0] # which is now also the first element in t!
'\x05'
This can be very helpful if you want to have more than one view on the data and don't want to (or can't) hold multiple copies in memory.
Note that buffer has been replaced by the better named memoryview in Python 3, though you can use either in Python 2.7.
Note also that you can't implement a buffer interface for your own objects without delving into the C API, i.e. you can't do it in pure Python.
I think buffers are e.g. useful when interfacing Python to native libraries (Guido van Rossum explains buffer in this mailing list post).
For example, NumPy seems to use buffer for efficient data storage:
import numpy
a = numpy.ndarray(1000000)
The a.data is a:
<read-write buffer for 0x1d7b410, size 8000000, offset 0 at 0x1e353b0>