Nov-17-2017, 12:35 AM
Greetings.
This is my first thread on this forum. I am learning Python from a book named "Learn to Program Using Python" (I am a VERY experienced programmer and DBA, BTW.) In the chapter on I/O I wasted no time diverging from the book's example. My code reads a line from a file and then spits it out with a line count and length. Here is the code, with my line numbers:
$ ./files1.py
Input line[1] is 22 chars: This is the first line
Input line[2] is 18 chars: and this is second
Input line[3] is 35 chars: I'm a little teapot short and stout
Input line[4] is 36 chars: Pour me out into the forest primeval
Input line[5] is 36 chars: THe murmuing poines and the hemlocks
Next, the book discusses using readline() rather than readlines(). Of course I gotta try that (in line 9). And of course it barfs in my face. Here is the output of that:
Input line[1] is 1 chars: T
Input line[2] is 1 chars: h
Input line[3] is 1 chars: i
Input line[4] is 1 chars: s
Input line[5] is 1 chars:
Input line[6] is 1 chars: i
Input line[7] is 1 chars: s
Input line[8] is 1 chars:
Input line[9] is 1 chars: t
Input line[10] is 1 chars: h
Input line[11] is 1 chars: e
Input line[12] is 1 chars:
Input line[13] is 1 chars: f
Input line[14] is 1 chars: i
Input line[15] is 1 chars: r
Input line[16] is 1 chars: s
Input line[17] is 1 chars: t
Input line[18] is 1 chars:
Input line[19] is 1 chars: l
Input line[20] is 1 chars: i
Input line[21] is 1 chars: n
Input line[22] is 1 chars: e
Input line[23] is 0 chars:
There are two weirdnesses about this:
-- Rasputin (my middle name is Concise
) Paskudniak
This is my first thread on this forum. I am learning Python from a book named "Learn to Program Using Python" (I am a VERY experienced programmer and DBA, BTW.) In the chapter on I/O I wasted no time diverging from the book's example. My code reads a line from a file and then spits it out with a line count and length. Here is the code, with my line numbers:
1 #!/usr/bin/python 2 # files1.py - first program to play with files 3 # 4 filename = "justsome.txt" 5 6 inp = open(filename, "r") 7 lc = 0 8 9 for one_line in inp.readlines() : 10 lc += 1 # Bump up lines-read count 11 one_line = one_line.rstrip('\n') 12 ll = len(one_line) 13 print "Input line[%d] is %d chars: %s" % (lc, ll, one_line) 14 15 # Done printing... 16 # 17 inp.closeHere, the output looks just as expected:
$ ./files1.py
Input line[1] is 22 chars: This is the first line
Input line[2] is 18 chars: and this is second
Input line[3] is 35 chars: I'm a little teapot short and stout
Input line[4] is 36 chars: Pour me out into the forest primeval
Input line[5] is 36 chars: THe murmuing poines and the hemlocks
Next, the book discusses using readline() rather than readlines(). Of course I gotta try that (in line 9). And of course it barfs in my face. Here is the output of that:
Input line[1] is 1 chars: T
Input line[2] is 1 chars: h
Input line[3] is 1 chars: i
Input line[4] is 1 chars: s
Input line[5] is 1 chars:
Input line[6] is 1 chars: i
Input line[7] is 1 chars: s
Input line[8] is 1 chars:
Input line[9] is 1 chars: t
Input line[10] is 1 chars: h
Input line[11] is 1 chars: e
Input line[12] is 1 chars:
Input line[13] is 1 chars: f
Input line[14] is 1 chars: i
Input line[15] is 1 chars: r
Input line[16] is 1 chars: s
Input line[17] is 1 chars: t
Input line[18] is 1 chars:
Input line[19] is 1 chars: l
Input line[20] is 1 chars: i
Input line[21] is 1 chars: n
Input line[22] is 1 chars: e
Input line[23] is 0 chars:
There are two weirdnesses about this:
- It is accepting only one character at a time. I have googled this issue and found the answers wanting. And searching this forum with so general a string will get me a haystack when I need the needle.
- It stopped reading after the first line. Otherwise the output would have been a line for every blessed character in the input file.
- How do I make readline() read a full line at each call?
- Why did it stop after the first line?
- Not that serious here but why isn't the len() function a string method?
-- Rasputin (my middle name is Concise
