
September 4th, 2008, 04:35 PM
| | | Read Binary data
Hi guys,
I am trying to read a binary file created by the following matlab
command:
fid=fopen('a.bin','w','b'); fwrite(fid,a,'real*8'); fclose(fid);, and
wondering how to do it in Python. I googled it but still get
confused.
'b' in fopen is for 'big-endian', 'real*8' in fwrite is for 64bit
float.
Thank you very much!
Jinbo Wang | 
September 4th, 2008, 05:05 PM
| | | Re: Read Binary data
"Mars creature" wrote: Quote:
I am trying to read a binary file created by the following matlab
command:
fid=fopen('a.bin','w','b'); fwrite(fid,a,'real*8'); fclose(fid);, and
wondering how to do it in Python. I googled it but still get
confused.
'b' in fopen is for 'big-endian', 'real*8' in fwrite is for 64bit
float.
|
f = open("a.bin", "rb") # read binary data
s = f.read() # read all bytes into a string
import array, sys
a = array.array("f", s) # "f" for float
if sys.byteorder != "big":
a.byteswap()
</F> | 
September 4th, 2008, 05:35 PM
| | | Re: Read Binary data
On Sep 4, 12:03 pm, Fredrik Lundh <fred...@pythonware.comwrote: Quote:
"Mars creature" wrote: Quote:
I am trying to read a binary file created by the following matlab
command:
fid=fopen('a.bin','w','b'); fwrite(fid,a,'real*8'); fclose(fid);, and
wondering how to do it in Python. I googled it but still get
confused.
'b' in fopen is for 'big-endian', 'real*8' in fwrite is for 64bit
float.
| >
f = open("a.bin", "rb") # read binary data
s = f.read() # read all bytes into a string
>
import array, sys
>
a = array.array("f", s) # "f" for float
if sys.byteorder != "big":
a.byteswap()
>
</F>
| Thanks Fredrik! I appreciate it!
The only thing is that a = array.array("f", s) should be a =
array.array("d", s) as the data is double precision.
Thanks again! | 
September 4th, 2008, 06:05 PM
| | | Re: Read Binary data
Am Thu, 04 Sep 2008 18:03:54 +0200 schrieb Fredrik Lundh: Quote:
> Quote: |
> I am trying to read a binary file [...]
| >
>
f = open("a.bin", "rb") # read binary data
s = f.read() # read all bytes into a string
>
import array, sys
>
a = array.array("f", s) # "f" for float
if sys.byteorder != "big":
a.byteswap()
>
| For more complicated structures, the struct module may help.
HTH.
Martin |
Posting Rules
| You may not post new threads You may not post replies You may not post attachments You may not edit your posts HTML code is Off | | | | | | What is Bytes?
We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights.
Get the best answers to your questions from over network members.
|