写有numpy的阵列数据的原始二进制文件 [英] Write a raw binary file with NumPy array data

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

问题描述

我想一个numpy的float数组中的内容保存到一个原始的二进制文件作为符号的16位整数。我试图做到这一点使用<一个href=\"http://docs.scipy.org/doc/numpy/reference/generated/numpy.ndarray.tofile.html\">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天全站免登陆