Re: first, second, etc line of text file

2009-04-24 Thread Gabriel Genellina
En Thu, 23 Apr 2009 18:50:06 -0300, Scott David Daniels escribió: Gabriel Genellina wrote: En Wed, 25 Jul 2007 19:14:28 -0300, James Stroud escribió: [nice recipe to retrieve only certain lines of a file] I think your time machine needs an adjustment, it spits things almost two years la

Re: first, second, etc line of text file

2009-04-23 Thread Scott David Daniels
Gabriel Genellina wrote: En Wed, 25 Jul 2007 19:14:28 -0300, James Stroud escribió: Daniel Nogradi wrote: A very simple question: I currently use a cumbersome-looking way of getting the first, second, etc. line of a text file: to_get = [0, 3, 7, 11, 13] got = dict((i,s) for (i,s) in enumerate

Re: first, second, etc line of text file

2007-07-26 Thread Steve Holden
Mike wrote: > On Jul 26, 8:46 am, "Daniel Nogradi" <[EMAIL PROTECTED]> wrote: A very simple question: I currently use a cumbersome-looking way of getting the first, second, etc. line of a text file: for i, line in enumerate( open( textfile ) ): if i == 0: print

Re: first, second, etc line of text file

2007-07-26 Thread Mike
On Jul 26, 8:46 am, "Daniel Nogradi" <[EMAIL PROTECTED]> wrote: > > > A very simple question: I currently use a cumbersome-looking way of > > > getting the first, second, etc. line of a text file: > > > > for i, line in enumerate( open( textfile ) ): > > > if i == 0: > > > print 'First

Re: first, second, etc line of text file

2007-07-26 Thread Neil Cerutti
On 2007-07-25, George Sakkis <[EMAIL PROTECTED]> wrote: > For random access, the easiest way is to slurp all the file in > a list using file.readlines(). A lazy evaluation scheme might be useful for random access that only slurps as much as you need. class LazySlurper(object): r""" Lazily rea

Re: first, second, etc line of text file

2007-07-26 Thread Daniel Nogradi
> > A very simple question: I currently use a cumbersome-looking way of > > getting the first, second, etc. line of a text file: > > > > for i, line in enumerate( open( textfile ) ): > > if i == 0: > > print 'First line is: ' + line > > elif i == 1: > > print 'Second line is

Re: first, second, etc line of text file

2007-07-25 Thread Gabriel Genellina
En Wed, 25 Jul 2007 19:14:28 -0300, James Stroud <[EMAIL PROTECTED]> escribió: > Daniel Nogradi wrote: >> A very simple question: I currently use a cumbersome-looking way of >> getting the first, second, etc. line of a text file: > > to_get = [0, 3, 7, 11, 13] > got = dict((i,s) for (i,s) in enu

Re: first, second, etc line of text file

2007-07-25 Thread Paul Rubin
"Daniel Nogradi" <[EMAIL PROTECTED]> writes: > A very simple question: I currently use a cumbersome-looking way of > getting the first, second, etc. line of a text file: > > for i, line in enumerate( open( textfile ) ): > if i == 0: > print 'First line is: ' + line > elif i == 1:

Re: first, second, etc line of text file

2007-07-25 Thread Jeff McNeil
Yup, I actually had log files in mind, too. I process Apache logs from an 8-way cluster every 15 minutes. There are 32,000 sites on said cluster; that's a lot of log data! I didn't actually think anyone would go *try* to open("war_and_peace.txt").readlines().. I just meant it as a generalization

Re: first, second, etc line of text file

2007-07-25 Thread Jay Loden
Grant Edwards wrote: > On 2007-07-25, Jeff <[EMAIL PROTECTED]> wrote: > >> That might be a memory problem if you are running multiple processes >> regularly, such as on a webserver. > > I suppose if you did it in parallel 50 processes, you could use > up 250MB of RAM. Still not a big deal on man

Re: first, second, etc line of text file

2007-07-25 Thread James Stroud
Daniel Nogradi wrote: > A very simple question: I currently use a cumbersome-looking way of > getting the first, second, etc. line of a text file: > > for i, line in enumerate( open( textfile ) ): >if i == 0: >print 'First line is: ' + line >elif i == 1: >print 'Second line

Re: first, second, etc line of text file

2007-07-25 Thread Grant Edwards
On 2007-07-25, Jeff <[EMAIL PROTECTED]> wrote: > That might be a memory problem if you are running multiple processes > regularly, such as on a webserver. I suppose if you did it in parallel 50 processes, you could use up 250MB of RAM. Still not a big deal on many servers. A decent OS will swap

Re: first, second, etc line of text file

2007-07-25 Thread Bjoern Schliessmann
Grant Edwards wrote: > On 2007-07-25, Jeff McNeil <[EMAIL PROTECTED]> wrote: >> Depending on the size of your file, you can just use >> file.readlines. Note that file.readlines is going to read the >> entire file into memory, so don't use it on your plain-text >> version of War and Peace. > > I

Re: first, second, etc line of text file

2007-07-25 Thread Jeff
Grant, That might be a memory problem if you are running multiple processes regularly, such as on a webserver. -- http://mail.python.org/mailman/listinfo/python-list

Re: first, second, etc line of text file

2007-07-25 Thread Grant Edwards
On 2007-07-25, Jeff McNeil <[EMAIL PROTECTED]> wrote: > Depending on the size of your file, you can just use > file.readlines. Note that file.readlines is going to read the > entire file into memory, so don't use it on your plain-text > version of War and Peace. I don't think that would actually

Re: first, second, etc line of text file

2007-07-25 Thread Daniel Nogradi
Thanks all! I think I will stick to my original method because the files can be quite large and without reading the whole file into memory probably enumerate( open( textfile ) ) is the only way to access an arbitrary Nth line. -- http://mail.python.org/mailman/listinfo/python-list

Re: first, second, etc line of text file

2007-07-25 Thread George Sakkis
On Jul 25, 3:44 pm, "Daniel Nogradi" <[EMAIL PROTECTED]> wrote: > A very simple question: I currently use a cumbersome-looking way of > getting the first, second, etc. line of a text file: > > for i, line in enumerate( open( textfile ) ): > if i == 0: > print 'First line is: ' + line >

Re: first, second, etc line of text file

2007-07-25 Thread Jeff McNeil
Depending on the size of your file, you can just use file.readlines. Note that file.readlines is going to read the entire file into memory, so don't use it on your plain-text version of War and Peace. >>> f = open("/etc/passwd") >>> lines = f.readlines() >>> lines[5] '# lookupd DirectoryServices

Re: first, second, etc line of text file

2007-07-25 Thread Jeff
Files should be iterable on their own: filehandle = open('/path/to/foo.txt') for line in filehandle: # do something... But you could also do a generic lines = filehandle.readlines(), which returns a list of all lines in the file, but that's a bit memory hungry. -- http://mail.python.org/mai

first, second, etc line of text file

2007-07-25 Thread Daniel Nogradi
A very simple question: I currently use a cumbersome-looking way of getting the first, second, etc. line of a text file: for i, line in enumerate( open( textfile ) ): if i == 0: print 'First line is: ' + line elif i == 1: print 'Second line is: ' + line ...