6.11 读写二进制数组数据.pdfVIP

  • 9
  • 0
  • 约4.8千字
  • 约 5页
  • 2017-08-29 发布于浙江
  • 举报
6.11 读写二进制数组数据

6.11 读写⼆进制数组数据 问题 你想读写⼀个⼆进制数组的结构化数据到Python元组中。 解决⽅案 可以使⽤ struct 模块处理⼆进制数据。下⾯是⼀ ⽰例代码将⼀个Python元组列表 写⼊⼀个⼆进制⽂件,并使⽤ struct 将每个元组编码为⼀个结构体。 from struct import Struct def write_records(records, format, f): Write a sequence of tuples to a binary file of structures. record_struct = Struct(format) for r in records: f.write(record_struct.pack( r)) # Example if __name__ == __main__: records = [ (1, 2.3, 4 .5), (6, 7.8, 9.0), (12, 13.4, 56.7) ] with open(data.b, wb) as f: write_re

文档评论(0)

1亿VIP精品文档

相关文档