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 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>
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!
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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
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...
|
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...
|
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...
|
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...
|
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...
|
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
|
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...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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...
|
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...
|
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,...
|
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...
|
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...
|
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...
| |