Question about Reading from text file with Python's Array class

traveller3141 traveller3141 at gmail.com
Sun Dec 18 13:33:32 EST 2011


I've been trying to use the Array class to read 32-bit integers from a
file. There is one integer per line and the integers are stored as text.
For problem specific reasons, I only am allowed to read 2 lines (2 32-bit
integers) at a time.

To test this, I made a small sample file (sillyNums.txt) as follows;
109
345
2
1234556

To read this file I created the following test script (trying to copy
something I saw on Guido's blog -
http://neopythonic.blogspot.com/2008/10/sorting-million-32-bit-integers-in-2mb.html
):

import array
assert array.array('i').itemsize == 4

bufferSize = 2

f=open("sillyNums.txt","r")
data = array.array('i')
data.fromstring(f.read(data.itemsize* bufferSize))
print data


The output was nonsense:
array('i', [171520049, 171258931])

I assume this has something to do with my incorrectly specifying how the
various bit/bytes line up. Does anyone know if there's a simple explanation
of how I can do this correctly, or why I can't do it at all?

Thanks,
Steven
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20111218/78cf8736/attachment-0001.html>


More information about the Python-list mailing list