Dear all,
I've been trying for a while to read values from a binary file containing unsigned short integers ('H'), without much success beyond reading and appending each value in a for loop. There must be a better option...
What is the most efficient way to read from such a file and output an array('H')?
Thanks in advance,
- Mathieu
efficient use of ustruct.unpack?
Re: efficient use of ustruct.unpack?
Hard to know what you've done, what you're trying to do.
You say you are without much success, but then seem to go on and say that you are successfully reading values. What is it that you can't do?
No code or console output provided to give us a clue what you are doing.
Could you share a bit more about your situation with clarity, please?
You say you are without much success, but then seem to go on and say that you are successfully reading values. What is it that you can't do?
No code or console output provided to give us a clue what you are doing.
Could you share a bit more about your situation with clarity, please?
-
- Posts: 847
- Joined: Mon Nov 20, 2017 10:18 am
Re: efficient use of ustruct.unpack?
very easy you can unpack muitlpe items at once even if they r different types, for you it will be even easier as they r all the same types.
Something like this will unpack 100 integers into an array
Something like this will unpack 100 integers into an array
Code: Select all
format_string = "H" * 100
array_of_intergers = ustruct.unpack (format_string, string_2b_unpacked)
Re: efficient use of ustruct.unpack?
If it was cpython, the most efficient way would be https://docs.python.org/2/library/array ... y.fromfile
Re: efficient use of ustruct.unpack?
Sorry, I believed I was clear enough. I was trying to do something like this:cefn wrote: ↑Thu Feb 01, 2018 1:57 amHard to know what you've done, what you're trying to do.
You say you are without much success, but then seem to go on and say that you are successfully reading values. What is it that you can't do?
No code or console output provided to give us a clue what you are doing.
Could you share a bit more about your situation with clarity, please?
Code: Select all
import array, ustruct
with open('data.bin', 'wb') as f :
for x in range(100) :
f.write( ustruct.pack('H', x) )
with open('data.bin', 'rb') as f :
data = array.array('H', ustruct.unpack('H', f.read()))
Duh. Of course! Thanks a lot.OutoftheBOTS_ wrote: ↑Thu Feb 01, 2018 6:55 amvery easy you can unpack muitlpe items at once even if they r different types, for you it will be even easier as they r all the same types.
Something like this will unpack 100 integers into an array
Code: Select all
format_string = "H" * 100 array_of_intergers = ustruct.unpack (format_string, string_2b_unpacked)
Yes, that's what I tried first. I really wish that array.array.fromfile() were available in uPython!deshipu wrote: ↑Thu Feb 01, 2018 8:04 amIf it was cpython, the most efficient way would be https://docs.python.org/2/library/array ... y.fromfile
Thanks again everyone,
- Mathieu
-
- Posts: 463
- Joined: Wed Apr 08, 2015 5:19 am
Re: efficient use of ustruct.unpack?
A better way is this:OutoftheBOTS_ wrote: ↑Thu Feb 01, 2018 6:55 amCode: Select all
format_string = "H" * 100 array_of_intergers = ustruct.unpack (format_string, string_2b_unpacked)
Code: Select all
>>> import ustruct as struct
>>> struct.pack('10H', *range(10))
b'\x00\x00\x01\x00\x02\x00\x03\x00\x04\x00\x05\x00\x06\x00\x07\x00\x08\x00\t\x00'
>>> struct.unpack('10H', _)
(0, 1, 2, 3, 4, 5, 6, 7, 8, 9)