from struct import Struct
def escribir_datos(datos, formato, f):
estructura = Struct(formato)
for dato in datos:
f.write(estructura.pack(*dato))
def leer_datos(formato, f):
estructura = Struct(formato)
partes = iter(lambda: f.read(estructura.size), b'')
return (estructura.unpack(parte) for parte in partes)
# Punto de entrada:
if __name__ == '__main__':
datos = [(2, 3, 5), (7, 11, 13), (17, 19, 23)]
with open('datos.bin', 'wb') as f:
escribir_datos(datos, '<idd', f)
with open('datos.bin', 'rb') as f:
for dato in leer_datos('<idd', f):
print(dato)
Comments
0 B
|👍
/👎