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
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
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
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
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
> > 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
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
"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:
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
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
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
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
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
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
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
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
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
>
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
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
19 matches
Mail list logo