Python从门到精通(五):文件处理-05-二进制处理(python二进制文件的操作步骤)

网友投稿 303 2022-08-27


Python从门到精通(五):文件处理-05-二进制处理(python二进制文件的操作步骤)

用二进制的目的主要是为了节省空间,将一些数据转为二进制形式,有时还需要定入元组中。

一、将python元组写入到二进制文件中

def write_records(record_list, format, f): """ Write a sequence of tuples to a binary file of structures. :param record_list: :param format: :param f: :return: """ record_struct = Struct(format) for r in record_list: f.write(record_struct.pack(*r))# Exampleif __name__ == '__main__': records = [(1, 3.3, 7.5), (5, 9.8, 11.0), (16, 18.4, 66.7)] with open('data.b', 'wb') as f: write_records(records, '

二、读取二进制文件-块形式读取元组中

def write_records(record_list, format, f): """ Write a sequence of tuples to a binary file of structures. :param record_list: :param format: :param f: :return: """ record_struct = Struct(format) for r in record_list: f.write(record_struct.pack(*r))# Exampleif __name__ == '__main__': records = [(1, 3.3, 7.5), (5, 9.8, 11.0), (16, 18.4, 66.7)] with open('data.b', 'wb') as f: write_records(records, '

三、读取二进制文件-字符串形式

from struct import Structdef unpack_records(format, data): record_struct = Struct(format) return (record_struct.unpack_from(data, offset) for offset in range(0, len(data), record_struct.size))if __name__ == '__main__': with open('test.b', 'rb') as f: data = f.read() for rec in unpack_records('

四、结构体处理

from struct import Structrecord_struct = Struct('

五、迭代器处理

#固定块大小迭代器f = open('test.b', 'rb')chunks = iter(lambda: f.read(20), b'')print(chunks)for chk in chunks: print(f'chk is: {chk}')#上面的迭代器方法等同于下面的两个方法之和def read_records(format, f): record_struct = Struct(format) while True: chk = f.read(record_struct.size) if chk == b'': break yield record_struct.unpack(chk)def unpack_records(format, data): record_struct = Struct(format) return (record_struct.unpack(data[offset:offset + record_struct.size]) for offset in range(0, len(data), record_struct.size))

六、大量文件处理

from collections import namedtuple#为元组命名Record = namedtuple('Record', ['kind','x','y'])with open('test.p', 'rb') as f: record_list = (Record(*r) for r in read_records('

七、复杂文件处理(图片等)

test_list = [ [(1.0, 2.5), (3.5, 4.0), (2.5, 1.5)], [(7.0, 1.2), (5.1, 3.0), (0.5, 7.5), (0.8, 9.0)], [(3.4, 6.3), (1.2, 0.5), (4.6, 9.2)],]import structimport itertoolsdef write_polys(file_name, polys): # Determine bounding box flattened = list(itertools.chain(*polys)) min_x = min(x for x, y in flattened) max_x = max(x for x, y in flattened) min_y = min(y for x, y in flattened) max_y = max(y for x, y in flattened) with open(file_name, 'wb') as f: f.write(struct.pack('','!','@')): byte_order = format[0] format = format[1:] format = byte_order + format setattr(self, field_name, StructField(format, offset)) offset += struct.calcsize(format) setattr(self, 'struct_size', offset)class Structure(metaclass=StructureMeta): def __init__(self, bytedata): self._buffer = bytedata @classmethod def from_file(cls, f): return cls(f.read(cls.struct_size))class PolyHeader(Structure): _fields_ = [ ('','!','@')): byte_order = format[0] format = format[1:] format = byte_order + format setattr(self, field_name, StructField(format, offset)) offset += struct.calcsize(format) setattr(self, 'struct_size', offset)class Point(Structure): _fields_ = [ ('i', 'file_code'), # Big endian ('20s', 'unused'), ('i', 'file_length'), ('

版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:Python从门到精通(六):线程-01-线程(线程 Python)
下一篇:Java mysql特殊形式的查询语句详解
相关文章

 发表评论

暂时没有评论,来抢沙发吧~