I'm hoping someone can explain to me how to write a binary output to a text file.
I know that in matlab, it is pretty simple, as it has the 'fwrite' function which has the following format: fwrite(fileID,A,precision)
However, I'm not sure how to translate this to python. I currently have the data stored in a 2-D numpy array and would like to write it to the file in the same way that matlab does.
I have attempted it with the following:
#let Y be the numpy matrix
with open (filepath,'wb') as FileToWrite:
np.array(Y.shape).tofile(FileToWrite)
Y.T.tofile(FileToWrite)
however, this didn't lead to the desired result. The file size is way to large and the data is incorrectly formatted. Ideally i should be able to specificy the data format to be uint8 or uint16 as well.
Any help would be massively appreciated.
So, I figured it. The solution is as follows.
# let Y be the numpy matrix
with open(filepath, 'wb') as FileToWrite:
np.asarray(Y, dtype=np.uint8).tofile(FileToWrite)