[Tutor] re: Newbie with question about readfiles()
stuart_clemons@us.ibm.com
stuart_clemons@us.ibm.com
Fri, 26 Apr 2002 11:28:42 -0400
--0__=0ABBE134DFC19C9A8f9e8a93df938690918c0ABBE134DFC19C9A
Content-type: text/plain; charset=US-ASCII
Thanks to all who replied. Here's what I learned from this exercise.
Here's the question:
Just wondering how I would print only a given line in a text file. For
example, I would like to print the 8th line in this multi-line file, foo.
txt.
in_file = open ("foo.txt", "r")
for line in in_file.readlines():
print line # I know this prints all lines
print ???? # How do I print line 8 only ???
1) print [8] # Will print the 8th element of the
first line, not the 8th line.
2) The following construct, using readline( ) (read one line at a time)
will print the 8th element of the first line, not the 8th line
# This prints the 8th element of the first line
in_file = open ("foo.txt", "r")
line = in_file.readline()
print line[8] # prints the 8th element of the first line
3) Adding a counter to readline( ) can do it, but the solution in 4) below
using readlines( ) is more straightforward (in my opinion anyway)
wanted = 8
while wanted > 0:
line = in_file.readline() # note readline(), this reads one
line at a time
wanted = wanted -1
print line
4) This one works ! This construct, using readlines( ), will print the 8th
line
## This prints the 8th line
in_file = open("foo.txt", "r")
lines = in_file.readlines() # lines is a list with all lines now
print lines[7] # print the 8th line (Python
counts from 0)
Thanks again to all who replied. I definitely learned something. This
tutor list is a great resource for a newbie like me.
- Stuart
--0__=0ABBE134DFC19C9A8f9e8a93df938690918c0ABBE134DFC19C9A
Content-type: text/html; charset=US-ASCII
Content-Disposition: inline
<html><body>
<p>Thanks to all who replied. Here's what I learned from this exercise.<br>
<br>
Here's the question:<br>
<br>
Just wondering how I would print only a given line in a text file. For example, I would like to print the 8th line in this multi-line file, foo.txt.<br>
<br>
in_file = open ("foo.txt", "r")<br>
for line in in_file.readlines():<br>
print line # I know this prints all lines<br>
print ???? # How do I print line 8 only ???<br>
1) print [8] # Will print the 8th element of the first line, not the 8th line.<br>
<br>
2) The following construct, using readline( ) (read one line at a time) will print the 8th element of the first line, not the 8th line<br>
<br>
# This prints the 8th element of the first line<br>
in_file = open ("foo.txt", "r")<br>
line = in_file.readline()<br>
print line[8] # prints the 8th element of the first line<br>
<br>
3) Adding a counter to readline( ) can do it, but the solution in 4) below using readlines( ) is more straightforward (in my opinion anyway)<br>
<br>
<font size="4" face="Courier New">wanted = 8<br>
while wanted > 0:<br>
line = in_file.readline() # note readline(), this reads one line at a time<br>
wanted = wanted -1<br>
print line<br>
</font> <br>
<br>
4) This one works ! This construct, using readlines( ), will print the 8th line<br>
<br>
## This prints the 8th line<br>
in_file = open("foo.txt", "r")<br>
lines = in_file.readlines() # lines is a list with all lines now<br>
print lines[7] # print the 8th line (Python counts from 0)<br>
<br>
<br>
Thanks again to all who replied. I definitely learned something. This tutor list is a great resource for a newbie like me.<br>
<br>
- Stuart</body></html>
--0__=0ABBE134DFC19C9A8f9e8a93df938690918c0ABBE134DFC19C9A--