Re: [Tutor] read line x from a file

2005-01-22 Thread Danny Yoo
On Sat, 22 Jan 2005, Kent Johnson wrote: > Jay Loden wrote: > > One simple solution is to do: > > > > fle = open(file) > > contents = file.readlines() > > file.close() > > print contents[x] #or store this in a variable, whatever > > That is the simplest solution. If your file gets bigger and yo

Re: [Tutor] read line x from a file

2005-01-22 Thread Kent Johnson
Max Noel wrote: On Jan 22, 2005, at 13:53, Kent Johnson wrote: That is the simplest solution. If your file gets bigger and you don't want to read it all at once, you can use enumerate to iterate the lines and pick out the one you want: f = open(...) for i, line in enumerate(f): if i==targetLin

Re: [Tutor] read line x from a file

2005-01-22 Thread Max Noel
On Jan 22, 2005, at 13:53, Kent Johnson wrote: That is the simplest solution. If your file gets bigger and you don't want to read it all at once, you can use enumerate to iterate the lines and pick out the one you want: f = open(...) for i, line in enumerate(f): if i==targetLine: print lin

Re: [Tutor] read line x from a file

2005-01-22 Thread Kent Johnson
Jay Loden wrote: One simple solution is to do: fle = open(file) contents = file.readlines() file.close() print contents[x] #or store this in a variable, whatever That is the simplest solution. If your file gets bigger and you don't want to read it all at once, you can use enumerate to iterate t

Re: [Tutor] read line x from a file

2005-01-21 Thread Orri Ganel
Jay Loden wrote: One simple solution is to do: fle = open(file) contents = file.readlines() file.close() print contents[x] #or store this in a variable, whatever -Jay On Friday 21 January 2005 11:22, J. M. Strother wrote: I have a text file containing 336 records. I can read

Re: [Tutor] read line x from a file

2005-01-21 Thread Jay Loden
One simple solution is to do: fle = open(file) contents = file.readlines() file.close() print contents[x] #or store this in a variable, whatever -Jay On Friday 21 January 2005 11:22, J. M. Strother wrote: > I have a text file containing 336 records. > I can read and print out the whole file w

Re: [Tutor] read line x from a file

2005-01-21 Thread Orri Ganel
J. M. Strother wrote: I haveĀ  a text file containing 336 records. I can read and print out the whole file without any problem. What I want to do is read and print out one record only (chosen at random). So I need to get to record x, select it, and then print it (or store it in a variable)

[Tutor] read line x from a file

2005-01-21 Thread J. M. Strother
I have a text file containing 336 records. I can read and print out the whole file without any problem. What I want to do is read and print out one record only (chosen at random). So I need to get to record x, select it, and then print it (or store it in a variable). Can anyone tell me now to do