473,408 Members | 1,973 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,408 software developers and data experts.

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
Sep 4 '08 #1
3 5874
"Mars creature" wrote:
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>

Sep 4 '08 #2
On Sep 4, 12:03 pm, Fredrik Lundh <fred...@pythonware.comwrote:
"Mars creature" wrote:
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!
Sep 4 '08 #3
Am Thu, 04 Sep 2008 18:03:54 +0200 schrieb Fredrik Lundh:
>
> 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
Sep 4 '08 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

19
by: Holger Hasselbach | last post by:
- The value of the object allocated by the malloc function is used (7.20.3.3). - The value of any bytes in a new object allocated by the realloc function beyond the size of the old object are used...
3
by: nicolasg | last post by:
Hi, I'm trying to open a file (any file) in binary mode and save it inside a new text file. After that I want to read the source from the text file and save it back to the disk with its...
4
by: zl2k | last post by:
hi, there I have a appendable binary file of complex data structure named data.bin created by myself. It is written in the following format: number of Data, Data array Suppose I have...
6
by: zl2k | last post by:
hi, there I have a appendable binary file of complex data structure named data.bin created by myself. It is written in the following format: number of Data, Data array Suppose I have...
5
by: dm3281 | last post by:
Hello, I have a text report from a mainframe that I need to parse. The report has about a 2580 byte header that contains binary information (garbage for the most part); although there are a...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.