[Tutor] Re: Need some concepts re reading in lines of data

Denis Frère spirou@carolo.net
Thu, 23 Mar 2000 11:09:18 +0000


"Robin B. Lake" wrote:
> 
> The following code (spaces, not tabs) does not print out the "line"
> data:
> 
> #  Read the input file into a list of line-strings:
>         for line in fp.readlines():
>                 print line
>         #       Get the line-per-string into a list of words:
>                 if len(line) != 0:
>                     words = string.split(line)  # line is a string; words is a list of strings
>                     print "words=", words
> 
> but DOES print out the words data.
> 
> What subtle aspect of Python have I missed, please?

I tried your code in a small "testread.py" (I simply added a hrule to
get a clearer output) : it works just fine with Python 1.5.2 and Debian
GNU/Linux. 
I join the file and its output beneath.

Don't see why it wouldn't work for you. (Which OS, which version, ... ?)
Did you sing the Great Python Incantations ? ;-)

-----------------testread.py-----------------------------
#! /usr/bin/python

import os, string
mydir = """/home/spirou/src/python"""
myfile = "testread.py"
hrule = '=' * 70

fp = open(os.path.join(mydir, myfile), 'r')

for line in fp.readlines():
    print line
    # Get the line-per-string into a list of words:
    if len(line) != 0:
        words = string.split(line)
        # line is a string; words is a list of strings
        print "words=", words
    print hrule

-----------------testread's output------------------------
#! /usr/bin/python

words= ['#!', '/usr/bin/python']
======================================================================


words= []
======================================================================
import os, string

words= ['import', 'os,', 'string']
======================================================================
mydir = """/home/spirou/src/python"""

words= ['mydir', '=', '"""/home/spirou/src/python"""']
======================================================================
myfile = "testread.py"

words= ['myfile', '=', '"testread.py"']
======================================================================
hrule = '=' * 70

words= ['hrule', '=', "'='", '*', '70']
======================================================================


words= []
======================================================================
fp = open(os.path.join(mydir, myfile), 'r')

words= ['fp', '=', 'open(os.path.join(mydir,', 'myfile),', "'r')"]
======================================================================


words= []
======================================================================
for line in fp.readlines():

words= ['for', 'line', 'in', 'fp.readlines():']
======================================================================
    print line

words= ['print', 'line']
======================================================================
    # Get the line-per-string into a list of words:

words= ['#', 'Get', 'the', 'line-per-string', 'into', 'a', 'list', 'of',
'words:']
======================================================================
    if len(line) != 0:

words= ['if', 'len(line)', '!=', '0:']
======================================================================
        words = string.split(line)

words= ['words', '=', 'string.split(line)']
======================================================================
        # line is a string; words is a list of strings

words= ['#', 'line', 'is', 'a', 'string;', 'words', 'is', 'a', 'list',
'of', 'strings']
======================================================================
        print "words=", words

words= ['print', '"words=",', 'words']
======================================================================
    print hrule

words= ['print', 'hrule']
======================================================================



-- 
Denis Frère
COLnet : Internet - Intranets - Réseaux - Formations
  P3B  : Club Free-Pytho-Linuxien Carolorégien
         http://www.carolo.net