New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
File write examples are inadequate #37297
Comments
The examples provided for writing to files in trying: results in: The addition of examples printing a list or tuple |
Logged In: YES Here are a few thoughts: The % formatting operator is fully documented in 2.2.6.2 %f is a valid format: '%f' % 3.14 There are several oddities in the file.write error in your The post doesn't include enough information to be able to To debug the example, it would help to separate the |
Logged In: YES The comment that the example is incomplete is well >>> 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 Thus, and file.write() do not have the same relationship Hence, my request that the tutorial's examples of file |
1 similar comment
Logged In: YES The comment that the example is incomplete is well >>> 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 Thus, and file.write() do not have the same relationship Hence, my request that the tutorial's examples of file |
Logged In: YES When coded with the proper syntax, the example runs fine: >>> peakPres = {'meas1': 1, 'Unknown': 0}
>>> f = open('test1', 'w')
>>> for pres in peakPres:
f.write('%s: %s, ' % (pres, peakPres[pres]))
>>> f.close()
>>> open('test1').read()
'Unknown: 0, meas1: 1, ' To debug your code, please do the following: Note, the tutorial and library reference are stand-alone Recommend that you withdraw the bug report. |
Logged In: YES Raymond: I asked Andrew to file this bug report after he |
Logged In: YES I appear to have failed to clearly state the point of this Examining tutorial section 7.2: http://www.python.org/doc/current/tut/node9.html#SE The examples provided show only static strings. Example 1:
Example 2:
Including even one example with a variable of any type I.e. consider including the following example: Printing a variable:
>>> a = 123.456
>>> f.write( '%d' % (a))
and: Printing a tuple:
>>> b = ('This', 'is', 9876.654, 'not a string')
>>> f.write(str(b))
or printing the tuple without the brackets:
>>> for element in b
... f.write('%s ' % str(element)) I believe this to be an essential point to be made and No sir, I will not withdraw the bug report at this time as |
Logged In: YES That's cool. |
Logged In: YES I took a close look at this, and considered adding text like (1) the write() method of files takes strings. If you need (2) The current tutorial is simple and clear. It has just (3) So the question is whether debunking your error is kandrew, please feel free to correct me if I've missed an |
Logged In: YES There's another option, which I've just implemented: Add an Doc/tut/tut.tex revisions 1.257, 1.196.8.24 |
Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.
Show more details
GitHub fields:
bugs.python.org fields:
The text was updated successfully, but these errors were encountered: