[Tutor] Re: Readlines code.

Derrick 'dman' Hudson dman@dman13.dyndns.org
Tue Jul 1 22:08:02 2003


--fdj2RfSjLxBAspz7
Content-Type: text/plain; charset=us-ascii
Content-Disposition: inline
Content-Transfer-Encoding: quoted-printable

On Tue, Jul 01, 2003 at 09:45:58PM -0400, Cliff Martin wrote:
| Rick,
|=20
| Thanks for the help. I understand what you did and this wraps up the=20
| file for printing but once the stuff is printed it no longer exists in=20
| the assignment words.

Think about the scope of your variable 'words', when it exists and
when it is changed.

| f=3Dopen("c:/transfer/filename")
| for line in f.readlines():
|        words=3Dline.rstrip().split()

The variable is assigned to inside the loop.  That means that each
time through the loop it refers to a different object.  Once the loop
terminates, the variable still exists as it did in the last iteration
of the loop.

There are a few different ways of solving your problem, depending on
what you want to do and what your constraints are.  One possibility is
this :

words =3D [s.rstrip().split()  for s in f.readlines()]

With this, the variable 'words' will be a list with each element
representing a line in the file.  Each element of that list is a list
containing the separate strings yielded by split().  Note that this
stores the entire file in memory at once.  If the file is large then
this isn't suitable.

(note that your code, using readlines(), has the same problem anyways)

Another option is to process each line, one at a time, in the loop.
This way you never have the entire file in memory at once, you only
have the line currently being processed.  (this can lead to DoS
problems too if the file has excessively long lines)

f=3Dopen("c:/transfer/filename")
for line in f.xreadlines():     # note: use xreadlines() instead
    words=3Dline.rstrip().split()
    ... do something with 'words' here ...
=2E.. here we're all done, we don't have the data in memory, ...
=2E.. but we don't need it because we already processed all of it ...

HTH,
-D

--=20
Microsoft is to operating systems & security ....
                                     .... what McDonald's is to gourmet coo=
king
=20
http://dman13.dyndns.org/~dman/

--fdj2RfSjLxBAspz7
Content-Type: application/pgp-signature
Content-Disposition: inline

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.0.6 (GNU/Linux)
Comment: For info see http://www.gnupg.org

iEYEARECAAYFAj8CPmkACgkQiB6vp1xAVUBGigCeJ5CzMZVXSSK2RugnsvLO9obF
G/0An1xwJCAnVKrQAib46elNp/tvuH+X
=C+RI
-----END PGP SIGNATURE-----

--fdj2RfSjLxBAspz7--