Viewed times. How can I speed up reading 12 bit little endian packed data in Python? Add a comment. Active Oldest Votes. Thank you so much. Your solution speeded up the code execution a lot. Previously it took 2 min and now 2 sec. I found a solution that runs even a lot faster than this one. I used some aspects of your solution. Such layout, which is used while packing and unpacking data, is specified using format characters.
These format characters, along with their size, are shown below:. Here, hhl indicates short, short, and long int as the data format layout, as we can see in the output.
Another interesting approach is provided in the NumPy module. Using the fromfile function in this module, we can read binary data from files after specifying the format data using the dtype function. Here, from the sentence, it will read only four words. As shown in the output. To get the output, I have used print number. And to close the file, I have used file. In the below screenshot you can see the output. To read the byte from the file, I have used print byte.
Asked 9 years ago. Active 9 months ago. Viewed 52k times. How do I read all the binary data in to an array of the structure? What is spectrum in your update? Did you mean dat2? Edited to reflect that. I like using Construct for things like this. It allows you to easily define and read complex structures in Python. Add a comment. Active Oldest Votes. Some readers may also be interested in reading the answers to the question Fastest way to read a binary file with a defined format?
I get the following error when I try to use your suggestion: struct. Sorry I am new to python. This can be because the file has some sort of header or trailer, in it as well as the data that comprises the array of structures or it's corrupt.
0コメント