如何使用 Python 编写 double 文件二进制文件?我目前正在执行以下操作,但它为我提供了单精度文件:
#!/usr/bin/env python
import struct
data =[2.3715231753176,9.342983274982732]
output_file = "test.dat"
out_file = open(output_file,"wb")
s = struct.pack('f'*len(data), *data)
out_file.write(s)
out_file.close()
最佳答案
使用d
写 double float :
s = struct.pack('d'*len(data), *data)
f
仅为单精度。查看Format characters struct
模块文档部分:
For the
'f'
and'd'
conversion codes, the packed representation uses the IEEE 754 binary32 (for'f'
) or binary64 (for'd'
) format, regardless of the floating-point format used by the platform.
关于python - 使用python编写 double 二进制文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16190115/