代码如下所示:
import struct class data_process(): def write_data(self,filename, data): fw = open(filename, "wb") for i in data: s = struct.pack('d', i) print('s',s) fw.write(s) fw.close() def read_data(self, filename, data): fr = open(filename, 'rb') unbytes = struct.unpack('{}d'.format(len(data)), fr.read()) print('unbytes',unbytes) return unbytes if __name__ == "__main__": data_p = data_process() a = [1, 2] data_p.write_data('data', a) data_p.read_data('data', a)
其中打包地时候的参数d表示double类型,更多类型参考以下博文:
https://www.cnblogs.com/qq78292959/archive/2013/04/07/3003607.html