[Python-bugs-list] [ python-Bugs-621057 ] File write examples are inadequate
noreply@sourceforge.net
noreply@sourceforge.net
Fri, 25 Oct 2002 12:50:04 -0700
Bugs item #621057, was opened at 2002-10-09 16:39
You can respond by visiting:
https://sourceforge.net/tracker/?func=detail&atid=105470&aid=621057&group_id=5470
Category: Documentation
Group: None
Status: Open
Resolution: None
Priority: 5
Submitted By: Andrew Stagg (kandrew)
Assigned to: Fred L. Drake, Jr. (fdrake)
Summary: File write examples are inadequate
Initial Comment:
The examples provided for writing to files in
section 7.2 of the documentation are overly simple
showing only writing of a static string. Reading
through 7.1 (although not actually discussing file
I/O) helps a little as the % operator is mentioned
but the "...C sprintf()-style format string..."
specifiers which are valid aren't cross referenced
(and the 'Library Reference' is a largish search
area). I had to find out experimentally that %f isn't
valid although %d is. To date, I haven't been able
to experimentally findout how to print a list or
tuple.
trying:
file.write('"%s"', str(relatedMeasurements))
results in:
TypeError: read-only buffer, tuple
The addition of examples printing a list or tuple
would be extremely helpful. Similarly, examples of
using file.write() to produce print type output
would be very illuminating.
----------------------------------------------------------------------
>Comment By: Andrew Stagg (kandrew)
Date: 2002-10-25 13:50
Message:
Logged In: YES
user_id=577467
The comment that the example is incomplete is well
founded. Therefore I attach a standalone example of
the problem I ran into:
>>> peakPres = {'Unknown':0}
>>> peakPres['meas1'] = 0
>>> peakPres['meas1'] = peakPres['meas1']+1
>>> print peakPres
{'meas1': 1, 'Unknown': 0}
>>> for pres in peakPres.keys():
... print pres, peakPres[pres]
...
meas1 1
Unknown 0
>>> file.open('test1.dat', 'w')
Traceback (innermost last):
File "<interactive input>", line 1, in ?
NameError: file
>>> file = open('test1.dat', 'w')
>>> for pres in peakPres.keys():
... file.write('%s: %s, ', pres, peakPres[pres])
...
Traceback (innermost last):
File "<interactive input>", line 2, in ?
TypeError: read-only buffer, tuple
>>>
Note that the call to print works while the call to
file.write does not. The reason is that the print
statement makes a call to __str__.
Thus, and file.write() do not have the same relationship
as C++ stream classes ostream and ofstream or C's
printf and fprintf. (As implied by rhettinger and the
current contents of the tutorial.)
Hence, my request that the tutorial's examples of file
output be expanded to the non-trival level of writing
list and dictionary members. Cross referenceing the
legal format specifiers (in the language reference) from
the tutorial examples would also be extremely helpful.
----------------------------------------------------------------------
Comment By: Andrew Stagg (kandrew)
Date: 2002-10-25 13:48
Message:
Logged In: YES
user_id=577467
The comment that the example is incomplete is well
founded. Therefore I attach a standalone example of
the problem I ran into:
>>> peakPres = {'Unknown':0}
>>> peakPres['meas1'] = 0
>>> peakPres['meas1'] = peakPres['meas1']+1
>>> print peakPres
{'meas1': 1, 'Unknown': 0}
>>> for pres in peakPres.keys():
... print pres, peakPres[pres]
...
meas1 1
Unknown 0
>>> file.open('test1.dat', 'w')
Traceback (innermost last):
File "<interactive input>", line 1, in ?
NameError: file
>>> file = open('test1.dat', 'w')
>>> for pres in peakPres.keys():
... file.write('%s: %s, ', pres, peakPres[pres])
...
Traceback (innermost last):
File "<interactive input>", line 2, in ?
TypeError: read-only buffer, tuple
>>>
Note that the call to print works while the call to
file.write does not. The reason is that the print
statement makes a call to __str__.
Thus, and file.write() do not have the same relationship
as C++ stream classes ostream and ofstream or C's
printf and fprintf. (As implied by rhettinger and the
current contents of the tutorial.)
Hence, my request that the tutorial's examples of file
output be expanded to the non-trival level of writing
list and dictionary members. Cross referenceing the
legal format specifiers (in the language reference) from
the tutorial examples would also be extremely helpful.
----------------------------------------------------------------------
Comment By: Raymond Hettinger (rhettinger)
Date: 2002-10-23 07:58
Message:
Logged In: YES
user_id=80475
Here are a few thoughts:
The % formatting operator is fully documented in 2.2.6.2
of the library reference. I think the tutorial should be kept
at an introductory level and the details left in the reference.
OTOH, I agree that 2.2.6.2 is hard to find.
%f is a valid format: '%f' % 3.14
There are several oddities in the file.write error in your
post. First, the double quotes inside the single quotes
(perhaps this is intentional). Second, the comma should
have been the % formatting operator. Third, since 'file' is
the name of a type, it is normally a good idea to pick
another name like 'infil' or 'outfil'.
The post doesn't include enough information to be able to
reproduce the error. We need to know the value of
relatedMeasurements and have a line showing how the file
was opened.
To debug the example, it would help to separate the
concerns of file writing from the string formatting. Get the
program to successfully format and print a string. Only
then, add a line to write it out to a file.
----------------------------------------------------------------------
You can respond by visiting:
https://sourceforge.net/tracker/?func=detail&atid=105470&aid=621057&group_id=5470