使用 NumPy 数组数据编写原始二进制文件

2022-01-09 00:00:00 python numpy binary

问题描述

我想将 numpy 浮点数组的内容作为有符号 16 位整数保存到原始二进制文件中.我尝试使用 ndarray.tofile 来完成此操作但我无法找出正确的格式字符串.似乎文件以双重格式保存,无论我如何选择格式字符串.我该怎么做呢?谢谢.

I'd like to save the contents of a numpy float array into a raw binary file as signed 16 bit integers. I tried to accomplish this using ndarray.tofile but I can't figure out the right format string. It seems that the file is saved in double format, mo matter how I choose the format string. How do I do this? Thanks.


解决方案

我认为最简单的方法是先将数组转换为int16,

I think the easiest way to do this is to first convert the array to int16,

array.astype('int16').tofile(filename)

相关文章