Movatterモバイル変換
[0]ホーム
Simple looping question...
David Allenmda at idatar.com
Mon Apr 2 19:57:11 EDT 2001
In article <mailman.986227337.10718.python-list at python.org>, "Vincent A.Primavera" <vincent_a_primavera at netzero.net> wrote:> Hello,> In a piece of code similar to this I am trying to read all the lines from a> file but no more. How can I set the "range" of this loop to stop when it> reaches the end of the file?>> for i in range(???):> a = fil1.readline()> print aYou're going at it from the wrong way around. If you really want to do it based on the number of linesin the file, do this:lines = fil1.readlines()for i in range(0, len(lines)): print lines[i]but better would probably be to do this:for line in fil1.readlines(): print lineIIRC, that way you don't have to have the whole damnfile in memory. Python is smart enough to assignlines one at a time into 'line' rather than to create an actual array.-- David Allenhttp://opop.nols.com/----------------------------------------Great minds run in great circles.
More information about the Python-listmailing list
[8]ページ先頭