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


Groups > comp.lang.python > #27663

Re: writelines puzzle

Path csiph.com!usenet.pasdenom.info!aioe.org!news.stack.nl!newsfeed.xs4all.nl!newsfeed6.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail
Return-Path <python-python-list@m.gmane.org>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.001
X-Spam-Evidence '*H*': 1.00; '*S*': 0.00; 'interpreter': 0.04; 'output': 0.04; 'method,': 0.07; 'parsing': 0.07; "'w')": 0.09; 'name):': 0.09; 'neighborhood': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'looked': 0.10; 'file,': 0.15; '...,': 0.16; 'entries.': 0.16; 'iterates': 0.16; 'numpy': 0.16; 'received:80.91.229.3': 0.16; 'received:dip.t-dialin.net': 0.16; 'received:plane.gmane.org': 0.16; 'received:t-dialin.net': 0.16; 'str()': 0.16; 'wing': 0.16; 'wrote:': 0.17; 'certainly': 0.17; '>>>': 0.18; 'input': 0.18; 'file.': 0.20; 'written': 0.20; 'meant': 0.21; 'fraction': 0.22; "i'd": 0.22; 'header:User- Agent:1': 0.26; 'values': 0.26; 'header:X-Complaints-To:1': 0.28; 'lines': 0.28; 'arrays': 0.29; 'assert': 0.29; 'cat': 0.29; 'decimal': 0.29; 'overhead': 0.29; 'array': 0.29; 'probably': 0.29; "i'm": 0.29; 'lists': 0.31; 'code': 0.31; 'point': 0.31; 'file': 0.32; 'print': 0.32; 'to:addr:python-list': 0.33; 'typically': 0.33; 'entry': 0.33; 'list': 0.35; 'something': 0.35; 'received:org': 0.36; 'but': 0.36; 'characters': 0.36; 'should': 0.36; 'correctly': 0.37; 'moment': 0.37; 'rather': 0.37; 'data': 0.37; 'subject:: ': 0.38; 'sure': 0.38; 'instead': 0.39; 'to:addr:python.org': 0.39; 'apply': 0.39; 'header:Received:5': 0.40; 'your': 0.60; 'qualified': 0.60; 'skip:w 30': 0.61; 'further': 0.61; 'save': 0.61; 'telling': 0.61; 'day.': 0.63; 'skip:n 10': 0.63; 'become': 0.65; 'decided': 0.65; 'middle': 0.66; 'skip:n 30': 0.69; 'analysis': 0.70; 'increase': 0.72; 'day': 0.73; 'william': 0.78; '160': 0.84
X-Injected-Via-Gmane http://gmane.org/
To python-list@python.org
From Peter Otten <__peter__@web.de>
Subject Re: writelines puzzle
Date Wed, 22 Aug 2012 19:44:02 +0200
Organization None
References <290CC961-63B7-4B9B-8822-AE7CBB5A5AED@mac.com>
Mime-Version 1.0
Content-Type text/plain; charset="ISO-8859-1"
Content-Transfer-Encoding 7Bit
X-Gmane-NNTP-Posting-Host p50849047.dip.t-dialin.net
User-Agent KNode/4.7.3
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.12
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <http://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <http://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.3672.1345657454.4697.python-list@python.org> (permalink)
Lines 73
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1345657454 news.xs4all.nl 6918 [2001:888:2000:d::a6]:36812
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:27663

Show key headers only | View raw


William R. Wing (Bill Wing) wrote:

> In the middle of a longer program that reads and plots data from a log
> file, I have added the following five lines (rtt_data is fully qualified
> file name):
> 
> wd = open(rtt_data, 'w')
> stat = wd.write(str(i))
> stat = wd.writelines(str(x_dates[:i]))
> stat = wd.writelines(str(y_rtt[:i]))
> wd.close()
> 
> The value of i is unknown before I have read through the input log file,
> but is typically in the neighborhood of 2500.  x_dates is a list of time
> stamps from the date2num method, that is values of the form
> 734716.72445602, day number plus decimal fraction of a day.  y_rtt is a
> list of three- or four-digit floating point numbers.  The x_dates and
> y_rtt lists are complete and plot correctly using matplotlib.  Reading and
> parsing the input log file and extracting the data I need is time
> consuming, so I decided to save the data for further analysis without the
> overhead of reading and parsing it every time.
> 
> Much to my surprise, when I looked at the output file, it only contained
> 160 characters.  Catting produces:
> 
> StraylightPro:Logs wrw$ cat RTT_monitor.dat
> 2354[ 734716.72185185  734716.72233796  734716.72445602 ..., 
> 734737.4440162
>   734737.45097222  734737.45766204][ 240.    28.5   73.3 ...,   28.4  
>   27.4   26.4]
> 
> Clearly I'm missing something fundamental about using the writelines
> method, and I'm sure it will be a DUH moment for me, but I'd sure
> appreciate someone telling me how to get that data all written out.  I
> certainly don't insist on writelines, but I would like the file to be
> human-readable.

When you apply str() to a numpy array big arrays are helpfully truncated, 
probably because the functionality is meant to be used in the interactive 
interpreter rather than to write to a file.
The default value is 1000 entries. One way to get the desired output is to 
increase the threshold:

>>> numpy.set_printoptions(threshold=4)
>>> print numpy.arange(10)
[0 1 2 ..., 7 8 9]
>>> numpy.set_printoptions(threshold=10)
>>> print numpy.arange(10)
[0 1 2 3 4 5 6 7 8 9]

Also, in

file.writelines(some_str)

writelines iterates over the characters of the some_string, so you should 
instead write the above as

file.write(some_str)

Your code will become 

assert numpy.get_printoptions()["threshold"] >= i
wd.write(str(x_dates[:i]))

If you intended to write one array entry at a time with writelines() here's 
how to do that:

wd.write("[")
wd.writelines("%s " % x for x in x_dates[:i])
wd.write("]\n")

numpy.savetxt() may also suit your needs.

Back to comp.lang.python | Previous | Next | Find similar | Unroll thread


Thread

Re: writelines puzzle Peter Otten <__peter__@web.de> - 2012-08-22 19:44 +0200

csiph-web