使用 NumPy 数组数据编写原始二进制文件 [英] Write a raw binary file with NumPy array data

查看:20
本文介绍了使用 NumPy 数组数据编写原始二进制文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将 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)

这篇关于使用 NumPy 数组数据编写原始二进制文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆