Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #17469 > unrolled thread

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

Started byPeter Otten <__peter__@web.de>
First post2011-12-18 20:24 +0100
Last post2011-12-18 20:24 +0100
Articles 1 — 1 participant

Back to article view | Back to comp.lang.python

This discussion starts older than the indexed window; earlier articles aren't shown. The article labeled Started by below is the oldest one visible, not the original post.


Contents

  Re: Question about Reading from text file with Python's Array class Peter Otten <__peter__@web.de> - 2011-12-18 20:24 +0100

#17469 — Re: Question about Reading from text file with Python's Array class

FromPeter Otten <__peter__@web.de>
Date2011-12-18 20:24 +0100
SubjectRe: Question about Reading from text file with Python's Array class
Message-ID<mailman.3801.1324236272.27778.python-list@python.org>
traveller3141 wrote:

> 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

These are numbers in ascii not in binary. To read these you don't have to 
use the array class:

from itertools import islice
data = []
with open("sillyNums.txt") as f:
    for line in islice(f, 2):
        data.append(int(line))

(If you insist on using an array you of course can)

"Binary" denotes the numbers as they are stored in memory and typically used 
by lowlevel languages like C. A binary 32 bit integer always consists of 4 
bytes. With your code

> 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])

you are telling Python to interpret the first 8 bytes in the file as two 
integers. The first 4 bytes are "1", "0", "9", "\n" (newline) when 
interpreted as characters or 49, 48, 57, 10 when looking at the bytes' 
numerical values. A 32 bit integer is calculated with

>>> 49+48*2**8+57*2**16+10*2**24
171520049

Looks familiar...

[toc] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web