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

时间:2022-11-25
本文介绍了使用 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 数组数据编写原始二进制文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

上一篇:如何修改整数中的位? 下一篇:从十六进制转换为二进制而不丢失前导0的python

相关文章

最新文章