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


Groups > comp.lang.python > #27655

Re: writelines puzzle

Path csiph.com!usenet.pasdenom.info!dedibox.gegeweb.org!gegeweb.eu!nntpfeed.proxad.net!proxad.net!feeder1-2.proxad.net!usenet-fr.net!nerim.net!novso.com!newsfeed.xs4all.nl!newsfeed6.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail
Return-Path <ckaynor@zindagigames.com>
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; 'output': 0.04; 'append': 0.07; 'method,': 0.07; 'parsing': 0.07; 'python': 0.09; "'w')": 0.09; '22,': 0.09; 'exception,': 0.09; 'expected.': 0.09; 'fails.': 0.09; 'garbage': 0.09; 'name):': 0.09; 'neighborhood': 0.09; 'received:mail-vb0-f46.google.com': 0.09; 'looked': 0.10; 'aug': 0.13; '(the': 0.15; 'file,': 0.15; '...,': 0.16; '2.7.3': 0.16; 'closed).': 0.16; 'formatting,': 0.16; 'guessing': 0.16; 'opened.': 0.16; 'wing': 0.16; 'wed,': 0.16; 'wrote:': 0.17; 'certainly': 0.17; 'written,': 0.17; 'thanks,': 0.18; 'input': 0.18; 'received:209.85.212.46': 0.18; 'variable': 0.20; 'to:name :python-list@python.org': 0.20; 'written': 0.20; 'fraction': 0.22; "i'd": 0.22; 'statement': 0.23; 'thus': 0.24; 'header:In-Reply- To:1': 0.25; 'values': 0.26; 'am,': 0.27; 'replace': 0.27; 'message-id:@mail.gmail.com': 0.27; 'lines': 0.28; 'received:209.85.212': 0.28; 'actual': 0.28; 'cat': 0.29; 'decimal': 0.29; 'end,': 0.29; 'overhead': 0.29; 'url:mailman': 0.29; 'case,': 0.29; "i'm": 0.29; 'lists': 0.31; 'point': 0.31; 'url:python': 0.32; 'file': 0.32; 'url:listinfo': 0.32; 'certain': 0.33; 'to:addr:python-list': 0.33; 'typically': 0.33; 'received:google.com': 0.34; 'list': 0.35; 'open': 0.35; 'doing': 0.35; 'received:209.85': 0.35; 'something': 0.35; 'but': 0.36; 'url:org': 0.36; 'closing': 0.36; 'correctly': 0.37; 'does': 0.37; 'being': 0.37; 'moment': 0.37; 'rather': 0.37; 'received:209': 0.37; 'data': 0.37; 'subject:: ': 0.38; 'sure': 0.38; 'to:addr:python.org': 0.39; 'header:Received:5': 0.40; 'url:mail': 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; 'more': 0.63; 'decided': 0.65; 'taking': 0.65; 'middle': 0.66; 'analysis': 0.70; 'day': 0.73; 'william': 0.78; 'absolutely': 0.84; '160': 0.84; 'preventing': 0.91; 'deal,': 0.93
X-Google-DKIM-Signature v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20120113; h=mime-version:in-reply-to:references:from:date:message-id:subject:to :content-type:content-transfer-encoding:x-gm-message-state; bh=qe/f/wIxjvfMtDCsRBovKt1r6xeQ9BHJV+bDYMbANkY=; b=i8aJTPEYudUMWSAX+yGD9db73ipu26kMJRrGe5+RwcjLxzzqunnh2v3wlXwlpyYVwR E6ZHiYlJtK3vTQu8c0L3PK+Pm61RMK8mI9zufPElzIZ3EIg2C+SLz29R/dheW69ZkaaZ rKRiEVBvNuzpBjRL8gCWW0fLue1QZAXpqdAvDvOKy5ZdAtsYSPksXPVCyL+cOwTTw8pb 8/wFYvKm9znuvsHTMgB78/efu0XlTKeBKW4yI1UJrVAOV1bMwjGh6oMG6ZSd5sJ5VSGH tWETX4qrkIoDm8iGHDcL1kg2S7XdqmkFYt27qM2OQNy3kmxkaYU7CnOAU1iH3hmELgli jurA==
MIME-Version 1.0
In-Reply-To <290CC961-63B7-4B9B-8822-AE7CBB5A5AED@mac.com>
References <290CC961-63B7-4B9B-8822-AE7CBB5A5AED@mac.com>
From Chris Kaynor <ckaynor@zindagigames.com>
Date Wed, 22 Aug 2012 09:48:41 -0700
Subject Re: writelines puzzle
To "python-list@python.org" <python-list@python.org>
Content-Type text/plain; charset=ISO-8859-1
Content-Transfer-Encoding quoted-printable
X-Gm-Message-State ALoCoQkeYOi13UtLzIVkMmzNLv0wa0OQXx0rVonMtc08IR1dNmLNygK5OwRa9vBnu1o1C40OST41
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.3666.1345654143.4697.python-list@python.org> (permalink)
Lines 71
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1345654143 news.xs4all.nl 6980 [2001:888:2000:d::a6]:38185
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:27655

Show key headers only | View raw


Reading your post, I do not see for sure what your actual issue is, so
I am taking my best guess: that the file does not contain as much data
as would be expected.

On Wed, Aug 22, 2012 at 8:38 AM, William R. Wing (Bill Wing)
<wrw@mac.com> 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')

Here, you are opening the data for write ("w"), which will replace the
contents of the file each time the file is opened. I am guessing you
want append ("a").

> stat = wd.write(str(i))
> stat = wd.writelines(str(x_dates[:i]))
> stat = wd.writelines(str(y_rtt[:i]))
> wd.close()

Also, rather than opening the file, writing to it, then closing it
manually, you would be better off using the with statement (presuming
Python 2.5+), like so:

with open(rtt_data, 'w') as wd:
    wd.write(str(i))
    wd.writelines(str(x_dates[:i]))
    wd.writelines(str(y_rtt[:i]))

In this case, you can be absolutely certain that the file will be
closed at the end, even if one of the commands in the middle fails.
The way you had it written, if one of the write/writeline, or any of
the formatting, fails, the file would be left open for an
indeterminate amount of time (the wd variable may be kept around as
part of the exception, preventing it from being garbage collected and
thus closed). Not a big deal, but more important if you are doing more
work with the file open.

>
> 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.
>
> Python 2.7.3
> OS-X 10.8
>
> Thanks,
> Bill
> --
> http://mail.python.org/mailman/listinfo/python-list

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


Thread

Re: writelines puzzle Chris Kaynor <ckaynor@zindagigames.com> - 2012-08-22 09:48 -0700

csiph-web