Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Delete lines containing a specific word

Reply
Thread Tools

Delete lines containing a specific word

 
 
Francesco Pietra
Guest
Posts: n/a
 
      01-06-2008
Please, how to adapt the following script (to delete blank lines) to delete
lines containing a specific word, or words?

f=open("output.pdb", "r")
for line in f:
line=line.rstrip()
if line:
print line
f.close()

If python in Linux accepts lines beginning with # as comment lines, please also
a script to comment lines containing a specific word, or words, and back, to
remove #.

Thanks
francesco pietra


__________________________________________________ __________________________________
Looking for last minute shopping deals?
Find them fast with Yahoo! Search. http://tools.search.yahoo.com/newsea...egory=shopping
 
Reply With Quote
 
 
 
 
Steven D'Aprano
Guest
Posts: n/a
 
      01-06-2008
On Sun, 06 Jan 2008 09:21:33 -0800, Francesco Pietra wrote:

> Please, how to adapt the following script (to delete blank lines) to
> delete lines containing a specific word, or words?


That's tricky, because deleting lines from a file isn't a simple
operation. No operating system I know of (Windows, Linux, OS X) has a
"delete line" function.

Do you really need to delete the lines in place? It would be much simpler
to leave the original data as-is, and create a new file with just the
lines that aren't deleted.


> f=open("output.pdb", "r")
> for line in f:
> line=line.rstrip()
> if line:
> print line
> f.close()


How to adapt this script:

First, think about what this script does. That is, it goes through each
line, and if the line is not blank, it prints it.

What do you want it to do instead? You want it to print the line if the
line doesn't contain a specific word. So that's the first thing you need
to change.

Secondly, you might want the script to write its output to a file,
instead of printing. So, instead of the line "print line", you want it to
write to a file.

Before you can write to a file, you need to open it. So you will need to
open another file: you will have two files open, one for input and one
for output. And you will need to close them both when you are finished.

Does that help you to adapt the script?


> If python in Linux accepts lines beginning with # as comment lines,
> please also a script to comment lines containing a specific word, or
> words, and back, to remove #.


The same process applies. Instead of "delete line", you want to "comment
line".



--
Steven


 
Reply With Quote
 
 
 
 
Dennis Lee Bieber
Guest
Posts: n/a
 
      01-06-2008
On Sun, 06 Jan 2008 21:09:41 -0000, Steven D'Aprano
<(E-Mail Removed)> declaimed the following in
comp.lang.python:

> That's tricky, because deleting lines from a file isn't a simple
> operation. No operating system I know of (Windows, Linux, OS X) has a
> "delete line" function.
>

How I miss the old SDS/XDS CP/V <G>

It did have a sort of delete-line if the text file had ever been run
through the normal editor... That's because the OS itself offered
transparent support for three file allocation/formats: consecutive
(essentially the common UNIX/Windows new-line formatted stream; could be
scattered across the disk space); keyed (ISAM keyed file, the default
key used by the editor being numeric, and mapped by FORTRAN IV into
direct access "record number"); and random (being a /contiguous/ disk
allotment with NO implied internal structure; fixed size upon creation
[or fail if the disk is too fragmented]).

So "delete-line" on a keyed/editor text file was possible, but might
lead to a need to compress/reindex the file at some point in time to
reclaim wasted space.


Of course, this was also the OS that had four file modes: read,
write, update (must read before writing), and scratch (must write before
reading) which could be applied to consecutive (stream) files. Update
and scratch both maintained two file position pointers, one for read
operations, one for write operations -- no need to do tell()/seek()
operations when switching from read to write; just had to ensure that
(for update) you have read the same or more than you plan to write.
--
Wulfraed Dennis Lee Bieber KD6MOG
http://www.velocityreviews.com/forums/(E-Mail Removed) (E-Mail Removed)
HTTP://wlfraed.home.netcom.com/
(Bestiaria Support Staff: (E-Mail Removed))
HTTP://www.bestiaria.com/
 
Reply With Quote
 
mik3l3374@gmail.com
Guest
Posts: n/a
 
      01-07-2008
On Jan 7, 1:21 am, Francesco Pietra <(E-Mail Removed)> wrote:
> Please, how to adapt the following script (to delete blank lines) to delete
> lines containing a specific word, or words?
>
> f=open("output.pdb", "r")
> for line in f:
> line=line.rstrip()
> if line:
> print line
> f.close()
>
> If python in Linux accepts lines beginning with # as comment lines, please also
> a script to comment lines containing a specific word, or words, and back, to
> remove #.
>
> Thanks
> francesco pietra
>
> __________________________________________________ __________________________________
> Looking for last minute shopping deals?
> Find them fast with Yahoo! Search. http://tools.search.yahoo.com/newsea...egory=shopping


for line in open("file"):
if not "word" in line:
print line


on the command, use the ">" operator to redirect to a file
 
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
Re: Delete lines containing a specific word Francesco Pietra Python 6 01-07-2008 03:57 AM
Re: Delete lines containing a specific word Matt Nordhoff Python 1 01-06-2008 07:15 PM
Fwd: Delete lines containing a specific word Francesco Pietra Python 0 01-06-2008 05:44 PM
Selecting directories containing specific fileglobs Eden Cardim Perl Misc 11 03-13-2006 04:42 PM
To delete few lines and add few lines at the end of a text file using c program Murali C++ 2 03-09-2006 04:45 PM



Advertisments