writelines puzzle

William R. Wing (Bill Wing) wrw at mac.com
Wed Aug 22 14:00:52 EDT 2012


On Aug 22, 2012, at 12:48 PM, Chris Kaynor <ckaynor at zindagigames.com> wrote:

> 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.
> 

Sorry, I should have been more explicit.  The value of "i" in this instance is 2354, so the file should (I thought) have contained the value of "i" followed by 2 x 2354 values of the data.

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

No, I really do want 'w'.  In the final package, this will be invoked once at the end of the month, before the log file is compressed and rolled.

[big snip]

>> 
>> Much to my surprise, when I looked at the output file, it only contained 160 characters [instead of ~2700 multi-character data values].  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]
>> 

Please look at what cat found in the file.  First there is the value of "i" as expected.  This is then followed by the first 3 values of the time stamp x-data, then a comma, an ellipsis, another comma, and the last three data values.  That patten (3 values, an ellipsis, and 3 final values) is repeated again for the y-data array/list.  It is almost as though "writelines" had written an editorial summary of the data rather than the data.

>> 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
> -- 
> http://mail.python.org/mailman/listinfo/python-list




More information about the Python-list mailing list