Re: reading specific lines of a file

2006-07-16 Thread Fredrik Lundh
John Machin wrote: >> 'sed' is not recognized as an internal or external command, >> operable program or batch file. > > aarrbejaysus #2: Download the installer from > > http://gnuwin32.sourceforge.net/packages/sed.htm in a way, this kind of advice reminds me of http://thedailywtf.co

Re: reading specific lines of a file

2006-07-16 Thread John Machin
On 16/07/2006 5:16 PM, Fredrik Lundh wrote: > Bill Pursell wrote: > >> Some might argue that this is not really doing >> it in Python. In fact, I would argue that! But if >> you're at a command prompt and you want to >> see line 7358, it's much easier to type >> % sed -n 7358p aarrbejaysus #1:

Re: reading specific lines of a file

2006-07-16 Thread Lawrence D'Oliveiro
In message <[EMAIL PROTECTED]>, Yi Xing wrote: > I want to read specific lines of a huge txt file (I know the line #). > Each line might have different sizes. Is there a convenient and fast > way of doing this in Python? Thanks. file("myfile.txt").readlines()[LineNr] Convenient, yes. Fast, n

Re: reading specific lines of a file

2006-07-16 Thread Lawrence D'Oliveiro
In message <[EMAIL PROTECTED]>, Fredrik Lundh wrote: > Bill Pursell wrote: > >> Some might argue that this is not really doing >> it in Python. In fact, I would argue that! But if >> you're at a command prompt and you want to >> see line 7358, it's much easier to type >> % sed -n 7358p >> than

Re: reading specific lines of a file

2006-07-16 Thread Fredrik Lundh
Bill Pursell wrote: > Some might argue that this is not really doing > it in Python. In fact, I would argue that! But if > you're at a command prompt and you want to > see line 7358, it's much easier to type > % sed -n 7358p > than it is to write the python one-liner. 'sed' is not recognized as

Re: reading specific lines of a file

2006-07-15 Thread John Machin
On 16/07/2006 2:54 PM, Nick Vatamaniuc top-posted: > Yi, > Use the linecache module. Yi, *don't* use the linecache module without carefully comparing the documentation and the implementation with your requirements. You will find that you have the source code on your computer -- mine (Windows bo

Re: reading specific lines of a file

2006-07-15 Thread Nick Vatamaniuc
Yi, Use the linecache module. The documentation states that : """ The linecache module allows one to get any line from any file, while attempting to optimize internally, using a cache, the common case where many lines are read from a single file. >>> import linecache >>> linecache.getline('/etc/pas

Re: reading specific lines of a file

2006-07-15 Thread bearophileHUGS
Pierre Quentel: > If the line number of the first line is 0 : > source=open('afile.txt') > for i,line in enumerate(source): > if i == line_num: > break > print line I don't know if something like this can be called an improvement: from itertools import islice afile = file('data.txt')

Re: reading specific lines of a file

2006-07-15 Thread Marc 'BlackJack' Rintsch
In <[EMAIL PROTECTED]>, Yi Xing wrote: > I want to read specific lines of a huge txt file (I know the line #). > Each line might have different sizes. Is there a convenient and fast > way of doing this in Python? Thanks. Don't know how efficient the `linecache` module in the standard library is

Re: reading specific lines of a file

2006-07-15 Thread Simon Forman
Yi Xing wrote: > Hi All, > > I want to read specific lines of a huge txt file (I know the line #). > Each line might have different sizes. Is there a convenient and fast > way of doing this in Python? Thanks. > > Yi Xing I once had to do a lot of random access of lines in a multi gigabyte log file

Re: reading specific lines of a file

2006-07-15 Thread Bill Pursell
Yi Xing wrote: > I want to read specific lines of a huge txt file (I know the line #). > Each line might have different sizes. Is there a convenient and fast > way of doing this in Python? Thanks. #!/usr/bin/env python import os,sys line = int(sys.argv[1]) path = sys.argv[2] os.system("sed -n %d

Re: reading specific lines of a file

2006-07-15 Thread Piet van Oostrum
> Yi Xing <[EMAIL PROTECTED]> (YX) wrote: >YX> Hi All, >YX> I want to read specific lines of a huge txt file (I know the line #). Each >YX> line might have different sizes. Is there a convenient and fast way of >YX> doing this in Python? Thanks. Not fast. You have to read all preceding lines.

Re: reading specific lines of a file

2006-07-15 Thread Pierre Quentel
If the line number of the first line is 0 : source=open('afile.txt') for i,line in enumerate(source): if i == line_num: break print line Pierre -- http://mail.python.org/mailman/listinfo/python-list

reading specific lines of a file

2006-07-15 Thread Yi Xing
Hi All, I want to read specific lines of a huge txt file (I know the line #). Each line might have different sizes. Is there a convenient and fast way of doing this in Python? Thanks. Yi Xing -- http://mail.python.org/mailman/listinfo/python-list