Fhernd icon

leer-escribir-arreglos-binarios.py

Fhernd | PRO | 08/03/18 11:13:41 AM UTC | 0 ⭐ | 3822 👁️ | Never ⏰ | []
Python |

640 B

|

None

|

0 👍

/

0 👎

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

  •  icon
    01/01/70 12:00:00 AM UTC
    Plain Text |

    0 B

    |

    👍

    /

    👎