The*_*eer 3 python numpy bitstring
我正在float32用 numpy 的tofile().
float_num = float32(3.4353)
float_num.tofile('float_test.bin')
Run Code Online (Sandbox Code Playgroud)
它可以用 numpy's 读取fromfile(),但这不符合我的需要,我必须在bitstring模块的帮助下将其作为原始二进制文件读取。
所以我执行以下操作:
my_file = open('float_test.bin', 'rb')
raw_data = ConstBitStream(my_file)
float_num_ = raw_data.readlist('float:32')
print float_num
print float_num_
Run Code Online (Sandbox Code Playgroud)
输出:
3.4353
-5.56134659129e+32
Run Code Online (Sandbox Code Playgroud)
可能是什么原因?第二个输出也应该是3.4353或关闭。
问题是 numpyfloat32存储为小端,而位串默认实现是大端。解决方案是指定 little endian 作为数据类型。
my_file = open('float_test.bin', 'rb')
raw_data = ConstBitStream(my_file)
float_num_ = raw_data.readlist('floatle:32')
print float_num
print float_num_
Run Code Online (Sandbox Code Playgroud)
输出:
3.4353
3.43530011177
Run Code Online (Sandbox Code Playgroud)
关于位串数据类型的参考,这里。
| 归档时间: |
|
| 查看次数: |
4442 次 |
| 最近记录: |