2012-06-05 28 views
5

Voglio leggere quanti più blocchi da 24 bit possibile da un file. Come posso farlo utilizzando stringhe di bit ConstBitStream quando non lo faccio ora quanti pezzi ci sono?Come leggere il file completo con bitstring

Attualmente mi fare questo:

eventList = ConstBitStream(filename = 'events.dat') 
for i in range(1000) : 
    packet = eventList.read(24) 

(qui devo calcolare il numero di eventi in anticipo)

risposta

4

Si potrebbe leggere fino a quando un esibirci ReadError viene generato

try: 
    while True: 
     packet = eventList.read(24) 
except ReadError: 
    pass 
+0

'ReadError' è un'eccezione' bitstring'. Deve essere o importato da 'bitstring' o usato come' bitstring.ReadError'. – TheMeaningfulEngineer

3

Catching the ReadError è una risposta perfettamente buona, ma un altro modo è utilizzare il metodo cut, che restituisce un generatore per le stringhe di bit di una determinata lunghezza, quindi solo

for packet in eventList.cut(24): 

dovrebbe funzionare.

+0

Soluzione molto conveniente, grazie! – HWende