Re: Reading from text

2009-03-08 Thread Steve Holden
Aahz wrote:
 In article mailman.140.1234896250.11746.python-l...@python.org,
 Tim Chase  python.l...@tim.thechases.com wrote:
 Assuming this is a real task and not a homework problem, then
 I'd do it this way:

   $ cd [directory containing 50 test files]
   $ (for file in *; do head -n11 $file | tail -n5; done) 
 /path/to/results-file.txt
 I'd use sed:

   sed -ns 7,11p /source/path/*.txt /path/to/results.txt

 hard to get much more concise than that with any common tool :)
 
 But you do have to learn sed.  Avoiding that is precisely why I stick
 with Python.

+1 [cross-platform] QOTW
-- 
Steve Holden   +1 571 484 6266   +1 800 494 3119
Holden Web LLC http://www.holdenweb.com/
Want to know? Come to PyCon - soon! http://us.pycon.org/

--
http://mail.python.org/mailman/listinfo/python-list


Re: Reading from text

2009-03-06 Thread Aahz
In article mailman.140.1234896250.11746.python-l...@python.org,
Tim Chase  python.l...@tim.thechases.com wrote:
 Assuming this is a real task and not a homework problem, then
 I'd do it this way:
 
   $ cd [directory containing 50 test files]
   $ (for file in *; do head -n11 $file | tail -n5; done) 
 /path/to/results-file.txt

I'd use sed:

   sed -ns 7,11p /source/path/*.txt /path/to/results.txt

hard to get much more concise than that with any common tool :)

But you do have to learn sed.  Avoiding that is precisely why I stick
with Python.
-- 
Aahz (a...@pythoncraft.com)   * http://www.pythoncraft.com/

All problems in computer science can be solved by another level of 
indirection.  --Butler Lampson
--
http://mail.python.org/mailman/listinfo/python-list


Re: Reading from text

2009-03-06 Thread Paul McGuire
On Feb 17, 11:03 am, oamram oam...@gmail.com wrote:
 Hi All,
 new to python. i have a directory with about 50 text file and i need to
 iterate through them and get
 line 7 to 11 from each file and write those lines into another file(one file
 that will contain all lines).


import glob
file(output.txt,w).write('\n'.join( .join(f.readlines()
[7:11+1]) ) for f in glob.glob(targetdir/*.txt))

-- Paul
--
http://mail.python.org/mailman/listinfo/python-list


Re: Reading from text

2009-02-17 Thread JB

oamram a écrit :

Hi All,
new to python. i have a directory with about 50 text file and i need to
iterate through them and get
line 7 to 11 from each file and write those lines into another file(one file
that will contain all lines).


First create a function that read and parse one file

Then create a loop that call this function for each file in a directory

Modules to read :

http://www.python.org/doc/2.5.2/tut/node9.html#SECTION00921
http://docs.python.org/library/os.html


Julien
--
http://mail.python.org/mailman/listinfo/python-list


Re: Reading from text

2009-02-17 Thread bearophileHUGS
oamram:
 i have a directory with about 50 text file and i need to
 iterate through them and get
 line 7 to 11 from each file and write those lines into another
 file(one file that will contain all lines).

Files can be iterated line-by-line, so this idiom:

for line in file: ...

will give you the lines, with newline. Once you have debugged that,
you can use the standard module glob (http://docs.python.org/library/
glob.html#module-glob ) to iterate on files.

If you have more problems, show use the code you have written and we
may suggest improvements.

Bye,
bearophile
--
http://mail.python.org/mailman/listinfo/python-list


Re: Reading from text

2009-02-17 Thread MRAB

bearophileh...@lycos.com wrote:

oamram:

i have a directory with about 50 text file and i need to
iterate through them and get
line 7 to 11 from each file and write those lines into another
file(one file that will contain all lines).


Files can be iterated line-by-line, so this idiom:

for line in file: ...

will give you the lines, with newline. Once you have debugged that,
you can use the standard module glob (http://docs.python.org/library/
glob.html#module-glob ) to iterate on files.


[snip]
Or:

for index, line in enumerate(my_file): ...

where index will give you the line number (starting from 0, so you'll
want lines 6 to 10). You can break out of the loop when you have all the
lines you want.


If you have more problems, show use the code you have written and we
may suggest improvements.


--
http://mail.python.org/mailman/listinfo/python-list


Re: Reading from text

2009-02-17 Thread Grant Edwards
On 2009-02-17, oamram oam...@gmail.com wrote:

 i have a directory with about 50 text file and i need to
 iterate through them and get line 7 to 11 from each file and
 write those lines into another file(one file that will contain
 all lines).

Assuming this is a real task and not a homework problem, then
I'd do it this way:

  $ cd [directory containing 50 test files]
  $ (for file in *; do head -n11 $file | tail -n5; done) 
/path/to/results-file.txt

-- 
Grant Edwards   grante Yow! ... My pants just went
  at   on a wild rampage through a
   visi.comLong Island Bowling Alley!!
--
http://mail.python.org/mailman/listinfo/python-list


Re: Reading from text

2009-02-17 Thread Tim Chase

Assuming this is a real task and not a homework problem, then
I'd do it this way:

  $ cd [directory containing 50 test files]
  $ (for file in *; do head -n11 $file | tail -n5; done) 
/path/to/results-file.txt


I'd use sed:

  sed -ns 7,11p /source/path/*.txt /path/to/results.txt

hard to get much more concise than that with any common tool :)

-tkc




--
http://mail.python.org/mailman/listinfo/python-list


Re: Reading from text

2009-02-17 Thread Grant Edwards
On 2009-02-17, Tim Chase python.l...@tim.thechases.com wrote:
 Assuming this is a real task and not a homework problem, then
 I'd do it this way:
 
   $ cd [directory containing 50 test files]
   $ (for file in *; do head -n11 $file | tail -n5; done) 
 /path/to/results-file.txt

 I'd use sed:

sed -ns 7,11p /source/path/*.txt /path/to/results.txt

Well done.

-- 
Grant Edwards   grante Yow! does your DRESSING
  at   ROOM have enough ASPARAGUS?
   visi.com
--
http://mail.python.org/mailman/listinfo/python-list


Re: Reading from text file

2008-08-26 Thread Fredrik Lundh

A. Joseph wrote:


I want to read from text file, 25 lines each time i press enter key,
just like the python documentation.


you can use pydoc's pager from your program:

import pydoc

text = open(filename).read()
pydoc.pager(text)

/F

--
http://mail.python.org/mailman/listinfo/python-list