Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Reading from text

Reply
Thread Tools

Reading from text

 
 
oamram
Guest
Posts: n/a
 
      02-17-2009

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).

Cheers, Omer.
--
View this message in context: http://www.nabble.com/Reading-from-t...p22061427.html
Sent from the Python - python-list mailing list archive at Nabble.com.

 
Reply With Quote
 
 
 
 
JB
Guest
Posts: n/a
 
      02-17-2009
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/...00000000000000
http://docs.python.org/library/os.html


Julien
 
Reply With Quote
 
 
 
 
bearophileHUGS@lycos.com
Guest
Posts: n/a
 
      02-17-2009
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
 
Reply With Quote
 
MRAB
Guest
Posts: n/a
 
      02-17-2009
http://www.velocityreviews.com/forums/(E-Mail Removed) 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.
>

 
Reply With Quote
 
Tim Chase
Guest
Posts: n/a
 
      02-17-2009
> 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




 
Reply With Quote
 
Aahz
Guest
Posts: n/a
 
      03-06-2009
In article <(E-Mail Removed)>,
Tim Chase <(E-Mail Removed)> 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 ((E-Mail Removed)) <*> http://www.pythoncraft.com/

"All problems in computer science can be solved by another level of
indirection." --Butler Lampson
 
Reply With Quote
 
Paul McGuire
Guest
Posts: n/a
 
      03-06-2009
On Feb 17, 11:03*am, oamram <(E-Mail Removed)> 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
 
Reply With Quote
 
Steve Holden
Guest
Posts: n/a
 
      03-09-2009
Aahz wrote:
> In article <(E-Mail Removed)>,
> Tim Chase <(E-Mail Removed)> 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/

 
Reply With Quote
 
Steve Holden
Guest
Posts: n/a
 
      03-09-2009
Aahz wrote:
> In article <(E-Mail Removed)>,
> Tim Chase <(E-Mail Removed)> 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/

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
UnauthorizedAccessException when reading XML files (no problem when reading other file-types) blabla120@gmx.net ASP .Net 0 09-15-2006 02:08 PM
DVD ROM not reading DVDs but reading CDs sayeed.afzal@gmail.com Computer Information 0 05-22-2006 10:37 AM
Controlling text in a Text Area or Text leo ASP General 1 12-05-2005 01:13 AM
ASP.NET Reading problem (reading .xls) Wael Soliman ASP .Net 2 01-03-2005 05:33 PM
reading the DB vs. reading a text file...performance preference? Darrel ASP .Net 3 11-11-2004 02:27 PM



Advertisments