Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I struct.unpack many numbers at once

Tags:

python

struct

I want to put a bunch of packed integers into a file, e.g.:

for i in int_list:
    fp.write(struct.pack('<I', i))

Now I'd like to read them out into int_list. I could do this, but it seems inefficient:

data = fp.read()
int_list = []
for i in xrange(0, len(data), 4):
    int_list.append(struct.unpack('<I', data[i:i+4])[0])

Is there a more efficient way to do this?

like image 540
Ian Bicking Avatar asked Dec 11 '11 03:12

Ian Bicking


People also ask

What does struct Calcsize do?

calcsize() This function calculates the size of the String representation of struct with a given format string.

What is struct unpack?

struct. unpack (format, buffer) Unpack from the buffer buffer (presumably packed by pack(format, ...) ) according to the format string format. The result is a tuple even if it contains exactly one item. The buffer's size in bytes must match the size required by the format, as reflected by calcsize() .

What does struct Pack return?

In Python 2, struct. pack() always returned a string type. It is in Python 3 that the function, in certain cases, will return a bytes object. Due to a lack of support of byte objects with this function in Python 2, it considered both bytes and string to be the same when returning.

Does Python have struct?

Python offers several data types that you can use to implement records, structs, and data transfer objects.


2 Answers

You can do it more efficiently in both directions:

>>> import struct

>>> int_list = [0, 1, 258, 32768]
>>> fmt = "<%dI" % len(int_list)
>>> data = struct.pack(fmt, *int_list)
>>> data
'\x00\x00\x00\x00\x01\x00\x00\x00\x02\x01\x00\x00\x00\x80\x00\x00'

>>> # f.write(data)
... # data = f.read()
...

>>> fmt = "<%dI" % (len(data) // 4)
>>> new_list = list(struct.unpack(fmt, data))
>>> new_list
[0, 1, 258, 32768]
like image 88
John Machin Avatar answered Nov 14 '22 00:11

John Machin


array.array should be fast for this. You can specify the type of elements it contains - there are a few for integers (although IIUC only in machine endianness), and then use its fromfile method to read directly from a file.

like image 38
Eli Bendersky Avatar answered Nov 14 '22 00:11

Eli Bendersky