Help | Site Map
Connecting Tech Pros Worldwide
 
 
LinkBack Thread Tools
  #1  
Old September 4th, 2008, 04:35 PM
Mars creature
Guest
 
Posts: n/a
Default 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
  #2  
Old September 4th, 2008, 05:05 PM
Fredrik Lundh
Guest
 
Posts: n/a
Default 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>

  #3  
Old September 4th, 2008, 05:35 PM
Mars creature
Guest
 
Posts: n/a
Default 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!
  #4  
Old September 4th, 2008, 06:05 PM
mblume
Guest
 
Posts: n/a
Default 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
 

Bookmarks

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

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.
Post your question now . . .
It's fast and it's free

Popular Articles