Skip to main content
 首页 » 编程设计

python之使用python编写 double 二进制文件

2025年05月04日11Leo_wl

如何使用 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.