[英]Python, How to write a 2-d array to a binary File with precision (uint8, uint16)?
我希望有人可以向我解释如何将二进制 output 写入文本文件。
我知道在 matlab 中,它非常简单,因为它具有“fwrite” function,其格式如下:fwrite(fileID,A,precision)
但是,我不确定如何将其翻译为 python。 我目前将数据存储在二维 numpy 数组中,并希望以与 matlab 相同的方式将其写入文件。
我尝试了以下方法:
#let Y be the numpy matrix
with open (filepath,'wb') as FileToWrite:
np.array(Y.shape).tofile(FileToWrite)
Y.T.tofile(FileToWrite)
然而,这并没有导致预期的结果。 文件大小太大,数据格式不正确。 理想情况下,我也应该能够将数据格式指定为 uint8 或 uint16。
任何帮助将不胜感激。
所以,我想通了。 解决方法如下。
#let Y be the numpy matrix
with open (filepath,'wb') as FileToWrite:
np.asarray(Y, dtype=np.uint8).tofile(FileToWrite)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.