我已经将双精度数组保存为二进制文件
saving.swift
val items: [Double] = ...
if let outputStream = OutputStream(url: getFileUrl(), append: true) {
outputStream.open()
items.withUnsafeBytes {
let byteArray: [UInt8] = Array($0)
outputStream.write(byteArray, maxLength: byteArray.count)
}
outputStream.close()
}
我现在尝试使用python处理那些原始文件。如何使用python将这些字节读回到双数组中?
process.py 导入结构
f = open("data.bin", "rb")
try:
byte = True
while byte != "":
byte_arr = f.read(8)
print(byte_arr)
print(len(byte_arr))
U = struct.unpack("4H", byte_arr)
print(U)
# print(unpack_from(byte_arr))
finally:
f.close()