使用python编写双精度二进制文件

jha*_*ade 1 python double-precision

如何使用Python编写双精度文件二进制文件?我现在正在做以下事情,但它给了我单精度文件:

#!/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()
Run Code Online (Sandbox Code Playgroud)

Mar*_*ers 5

使用d写双精度浮点数:

s = struct.pack('d'*len(data), *data)
Run Code Online (Sandbox Code Playgroud)

f只精度.请参阅模块文档的格式字符部分struct:

对于'f''d'转换代码,打包表示使用IEEE 754 binary32(for 'f')或binary64(for 'd')格式,而不管平台使用的浮点格式.