Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #27656 > unrolled thread
| Started by | Joel Goldstick <joel.goldstick@gmail.com> |
|---|---|
| First post | 2012-08-22 12:49 -0400 |
| Last post | 2012-08-22 12:49 -0400 |
| 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.
Re: writelines puzzle Joel Goldstick <joel.goldstick@gmail.com> - 2012-08-22 12:49 -0400
| From | Joel Goldstick <joel.goldstick@gmail.com> |
|---|---|
| Date | 2012-08-22 12:49 -0400 |
| Subject | Re: writelines puzzle |
| Message-ID | <mailman.3667.1345654154.4697.python-list@python.org> |
On Wed, Aug 22, 2012 at 11: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') > 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. > > 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 = 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 = [1,2,3] >>> str(l) '[1, 2, 3]' >>> s = map(str, l) >>> s ['1', '2', '3'] -- Joel Goldstick
Back to top | Article view | comp.lang.python
csiph-web