Path: csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!weretis.net!feeder1.news.weretis.net!feeder.erje.net!newsfeed.xs4all.nl!newsfeed6.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.000 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'numerical': 0.05; 'subject:Python': 0.05; 'ascii': 0.07; 'bytes.': 0.07; 'python': 0.08; '32-bit': 0.09; 'assert': 0.09; 'integers': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:80.91.229.12': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'received:lo.gmane.org': 0.09; 'output': 0.10; 'subject:file': 0.12; 'binary': 0.13; 'stored': 0.13; '48,': 0.16; '49,': 0.16; '57,': 0.16; 'binary.': 0.16; 'class:': 0.16; 'denotes': 0.16; "guido's": 0.16; 'insist': 0.16; 'integers.': 0.16; 'received:dip.t-dialin.net': 0.16; 'received:t-dialin.net': 0.16; 'wrote:': 0.18; '>>>': 0.18; 'bytes': 0.18; 'memory': 0.21; 'trying': 0.21; 'subject:Question': 0.21; 'from:addr:web.de': 0.23; 'interpreted': 0.23; 'values.': 0.23; 'code': 0.25; 'import': 0.27; 'bit': 0.28; 'script': 0.28; 'interpret': 0.29; 'looks': 0.29; 'problem': 0.29; 'print': 0.29; 'class': 0.29; 'lines': 0.30; 'array': 0.30; 'calculated': 0.30; "i've": 0.31; 'subject:text': 0.32; 'typically': 0.32; 'header:X-Complaints- To:1': 0.33; 'there': 0.33; 'to:addr:python-list': 0.34; 'file.': 0.34; 'integer': 0.34; 'languages': 0.35; 'something': 0.35; 'test': 0.35; 'file': 0.36; 'subject:with': 0.36; 'two': 0.37; 'using': 0.38; 'received:org': 0.38; 'created': 0.38; 'subject:from': 0.38; 'skip:o 20': 0.38; 'characters': 0.39; 'to:addr:python.org': 0.40; 'your': 0.61; 'saw': 0.67; 'url:10': 0.70; 'skip:4 20': 0.93 X-Injected-Via-Gmane: http://gmane.org/ To: python-list@python.org From: Peter Otten <__peter__@web.de> Subject: Re: Question about Reading from text file with Python's Array class Date: Sun, 18 Dec 2011 20:24:28 +0100 Organization: None References: Mime-Version: 1.0 Content-Type: text/plain; charset="ISO-8859-1" Content-Transfer-Encoding: 7Bit X-Gmane-NNTP-Posting-Host: p5084955f.dip.t-dialin.net X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.12 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 58 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1324236272 news.xs4all.nl 6988 [2001:888:2000:d::a6]:35629 X-Complaints-To: abuse@xs4all.nl Xref: x330-a1.tempe.blueboxinc.net comp.lang.python:17469 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...