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!news2.euro.net!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.002 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'argument': 0.04; 'output': 0.04; 'method,': 0.07; 'parsing': 0.07; 'python': 0.09; "'w')": 0.09; '22,': 0.09; 'name):': 0.09; 'neighborhood': 0.09; 'cc:addr :python-list': 0.10; 'looked': 0.10; 'aug': 0.13; 'file,': 0.15; '...,': 0.16; '2.7.3': 0.16; 'received:mail-wi0-f178.google.com': 0.16; 'wing': 0.16; 'wed,': 0.16; 'wrote:': 0.17; 'certainly': 0.17; 'thanks,': 0.18; '>>>': 0.18; 'input': 0.18; 'file.': 0.20; 'written': 0.20; 'fraction': 0.22; "i'd": 0.22; 'cc:2**0': 0.23; 'this:': 0.23; 'second': 0.24; 'cc:addr:python.org': 0.25; 'header :In-Reply-To:1': 0.25; 'values': 0.26; 'am,': 0.27; 'message- id:@mail.gmail.com': 0.27; 'lines': 0.28; 'received:209.85.212': 0.28; 'cat': 0.29; 'decimal': 0.29; 'overhead': 0.29; 'url:mailman': 0.29; "i'm": 0.29; 'writes': 0.30; 'lists': 0.31; 'point': 0.31; 'url:python': 0.32; 'file': 0.32; 'url:listinfo': 0.32; 'typically': 0.33; 'received:google.com': 0.34; 'list': 0.35; 'received:209.85': 0.35; 'something': 0.35; 'list.': 0.35; 'but': 0.36; 'url:org': 0.36; 'correctly': 0.37; 'moment': 0.37; 'received:209': 0.37; 'data': 0.37; 'subject:: ': 0.38; 'perform': 0.38; 'sure': 0.38; 'header:Received:5': 0.40; 'url:mail': 0.40; 'qualified': 0.60; 'skip:w 30': 0.61; 'further': 0.61; 'save': 0.61; 'map': 0.61; 'telling': 0.61; 'first': 0.61; 'day.': 0.63; 'decided': 0.65; 'middle': 0.66; 'analysis': 0.70; 'day': 0.73; 'william': 0.78; "'2',": 0.84; '160': 0.84; 'joel': 0.91 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:date:message-id:subject:from:to :cc:content-type:content-transfer-encoding; bh=lT8XLDNUs/bfSxqdYu9n3gbBTiOlHVkYhG0m/1sbc9Q=; b=pDQXvdz8HVfMlUwmEgs0ehiynuoXi3WqHqe+6ocfdP2s0fXg3VzExOqryLtt8mdFky pm9Fa51hHmtGVr8Q1zgA28rAL2dymTYv+KuABHa8JWuUvF1Z/f6XvxO/BK6VyXPsZPkA M4LLcHHQ8Mxd6ecn8rafpmqcyeM6SsIkgecPefXdYsWGPb2Fg3QfAiH7b5rmr65Xje5D vWJFiZzeQue7Z3c0OGvK6XXfZW3KcK1cEtyfMG7lLC9MPClf7xvn+zKT7KeI3fhfK09c h54nMTtOfd62tZPtK2uZ08aI/nchlpi3w+wwE8NFBhfeUNLrHKzsxNb+JY3S8D0H2/G/ 1QgQ== MIME-Version: 1.0 In-Reply-To: <290CC961-63B7-4B9B-8822-AE7CBB5A5AED@mac.com> References: <290CC961-63B7-4B9B-8822-AE7CBB5A5AED@mac.com> Date: Wed, 22 Aug 2012 12:49:13 -0400 Subject: Re: writelines puzzle From: Joel Goldstick To: "William R. Wing (Bill Wing)" Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: quoted-printable Cc: "python-list@python.org" 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: 62 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1345654154 news.xs4all.nl 6869 [2001:888:2000:d::a6]:38314 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:27656 On Wed, Aug 22, 2012 at 11:38 AM, William R. Wing (Bill Wing) wrote: > In the middle of a longer program that reads and plots data from a log fi= le, I have added the following five lines (rtt_data is fully qualified file= name): > > wd =3D open(rtt_data, 'w') > stat =3D wd.write(str(i)) > stat =3D wd.writelines(str(x_dates[:i])) > stat =3D 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 st= amps 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 fo= ur-digit floating point numbers. The x_dates and y_rtt lists are complete = and plot correctly using matplotlib. Reading and parsing the input log fil= e and extracting the data I need is time consuming, so I decided to save th= e 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.4440= 162 > 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 meth= od, and I'm sure it will be a DUH moment for me, but I'd sure appreciate so= meone telling me how to get that data all written out. I certainly don't i= nsist 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 writelines writes a list of strings to a file. you are using this: > stat =3D wd.writelines(str(x_dates[:i])) which is the same as my second line below If you use map it will perform the first argument over the list. See if that works for you >>> l =3D [1,2,3] >>> str(l) '[1, 2, 3]' >>> s =3D map(str, l) >>> s ['1', '2', '3'] --=20 Joel Goldstick