Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Read/Write binary file
I have a binary file that contains 32 bit floats. I would like to read the file into a matrix, rotate the matrix, and then write the matrix to a file.

In the original file the values would be arranged like this:
a0, b0, a1, b1, a2, b2, a3, b3...aN, bN
where a,b can be thought of as rows and 1, 2, 3 as columns.

In the resulting file the values would be arranged like this:
a0, a1, a2, a3...aN, b0, b1, b2, b3...bN

I'm beginning to think I might need to use the array library to read and write the files, and write some C code to do the rotation. Does anyone know of other ways?
I don't think it's nearly as complicated as you think.
Please supply a sample file.
I don't know how to provide a sample file, so here is some code to generate the sample file and demonstrate the kind of manipulation I need.
import numpy as np
from array import array

a = array('f', [x for _ in range(10) for x in range(4)])
with open('sample_order.bin', 'wb') as file:

a = array('f')
with open('sample_order.bin', 'rb') as file:
    a.fromfile(file, 40)
print('Sample order file', a, sep='\n')

b = np.array(a)
print('As numpy array', b, sep='\n')
c = b.reshape((10, 4))
print('As a matrix', c, sep='\n')
d = c.transpose()
print('As matrix in channel order', d, sep='\n')
e = array('f', d.flatten())
print('Channel order file', e, sep='\n')

with open('channel_order.bin', 'wb') as file:
I start with an array like this:

I want to convert this to:

I am doing this by turning it into a matrix, transposing the matrix and then flattening the matrix, I know there has to be a better way.
for 2d array, I'd use something like below

But you have a single dimension array, so you're way ahead of me on this.

import numpy as np
from scipy.ndimage.interpolation import rotate
import os

# make sure file can be found in script directory

def rotate_and_save(infilename, outfilename, rotation_angle_degrees=90):    
    orig_array = np.fromfile(infilename, dtype=np.int64)
    print(f"\noriginal array\n{orig_array}")
    new_array = rotate(orig_array, angle=rotation_angle_degrees)
    print(f"\nnew array\n{new_array}")
    np.tofile(outfilename, dtype=np.int64)

def main():
    # Use any desired angle
    rotate_and_save('channel_order.bin', 'rotated_channel_order.bin', 

if __name__ == '__main__':

Possibly Related Threads…
Thread Author Replies Views Last Post
  Can't write to file Mark17 2 57 11 hours ago
Last Post: Mark17
  How to write in text file - indented block Joni_Engr 3 329 Aug-09-2021, 06:30 PM
Last Post: deanhystad
  Read csv file through PyCharm kimx0961 3 418 Aug-01-2021, 07:05 PM
Last Post: perfringo
  UART Serial Read & Write to MP3 Player Doesn't Work bill_z 15 868 Jul-17-2021, 04:19 PM
Last Post: bill_z
  Why it does not print( Rejaul84 1 393 Jul-01-2021, 10:37 PM
Last Post: bowlofred
  Read and write active Excel file euras 4 522 Jun-29-2021, 11:16 PM
Last Post: Pedroski55
  [Solved] Trying to read specific lines from a file Laplace12 7 757 Jun-21-2021, 11:15 AM
Last Post: Laplace12
  [Solved] Using readlines to read data file and sum columns Laplace12 4 502 Jun-16-2021, 12:46 PM
Last Post: Laplace12
  Read file, reformat and write new file bryanmartin113 1 509 Jun-08-2021, 09:27 PM
Last Post: Larz60+
  write mariadb table rows query to each file? shams 1 671 Feb-02-2021, 04:10 PM
Last Post: buran

Forum Jump:

User Panel Messages

Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020