473,246 Members | 1,380 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,246 software developers and data experts.

Delete lines containing a specific word

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
Jan 6 '08 #1
2 19692
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
Jan 6 '08 #2
On Jan 7, 1:21 am, Francesco Pietra <chiendar...@yahoo.comwrote:
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
Jan 7 '08 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: mahesr | last post by:
Frds, how to crawl a specific word in whole website using php.need which page it has Example: want to crawl a 144 items found,on somepage.html. Pls if u have code pls mail email address...
2
by: lekshminair | last post by:
U]can u solve it I am trying to take a specific word in textfile. for example: new .txt "Please follow these guidelines when posting questions. Submitting clear and concise questions allows...
8
by: Umesh | last post by:
Please help. THANKS.
0
by: AcidBurn | last post by:
Hi Can anyone help me ragarding my query Im trying to develop a classic asp script that will count the number of lines of ms word document. say...this is the content of the word file: ...
3
by: Robertf987 | last post by:
Well, I think I've described what I want to do in the title here. In the database, I have two main tables that contain the main data for the database. One for group expenditures, another for...
2
by: Kristapins | last post by:
Hello! Can anyone help me... I have *.txt file containing lines like... 100 0 0 7 high UNLOCKED CRITICAL Door open in site CLEARED 100 11 0 7 low UNLOCKED CRITICAL Door Open CLEARED 100 12 0...
0
by: Francesco Pietra | last post by:
I forgot to add that the lines to strip are in present case of the type of the following block HETATM 7007 O WAT 446 27.622 34.356 55.205 1.00 0.00 O HETATM 7008 H1 WAT...
2
by: weldeslasai | last post by:
I need help on preparation of c++ code that could serve as for finding specific word from.In other words the code for very simple dictionary code. Thanks
0
by: abbasky | last post by:
### Vandf component communication method one: data sharing ​ Vandf components can achieve data exchange through data sharing, state sharing, events, and other methods. Vandf's data exchange method...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: Aftab Ahmad | last post by:
Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...
0
by: Aftab Ahmad | last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below. Dim IE As Object Set IE =...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: marcoviolo | last post by:
Dear all, I would like to implement on my worksheet an vlookup dynamic , that consider a change of pivot excel via win32com, from an external excel (without open it) and save the new file into a...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.