Re: Delete lines containing a specific word

2008-01-07 Thread mik3l3374
On Jan 7, 1:21 am, Francesco Pietra <[EMAIL PROTECTED]> 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/newsearch/category.php?category=shopping

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


on the command, use the ">" operator to redirect to a file
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Delete lines containing a specific word

2008-01-06 Thread sj26


Francesco Pietra 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 #.
> 

Well the simplest way in python using the stream for data or configuration
or something IMHO would be to use a filter, list comprehension or generator
expression:



# filter (puts it all in memory)
lines = filter(lambda line: not line.lstrip().startswith('#'),
open("output.pdb", "r"))

# list comprehension (puts it all in memory)
lines = [line for line in open("output.pdb", "r") if not
line.lstrip().startswith('#')]

# generator expression (iterable, has .next(), not kept in memory)
lines = (line for line in open("output.pdb", "r") if not
line.lstrip().startswith('#'))


Check out  http://rgruet.free.fr/PQR25/PQR2.5.html 
http://rgruet.free.fr/PQR25/PQR2.5.html  for some quick hints.

-- 
View this message in context: 
http://www.nabble.com/Delete-lines-containing-a-specific-word-tp14651102p14660373.html
Sent from the Python - python-list mailing list archive at Nabble.com.
-- 
http://mail.python.org/mailman/listinfo/python-list

Re: Delete lines containing a specific word

2008-01-06 Thread Francesco Pietra
As I said I am no expert in OS and commands, except on what concerns mechanical
statistical and quantum mechanical calculations. Therefore, better for me (and
for all guys here) if I stop on this matter. My reply is only to say that I did
the job with:

f=open("prod1-3_no_wat_pop.pdb", "r")
for line in f:
line=line.rstrip()
if "WAT" not in line:
print line
f.close()

It took time on the billion lines to write, though it worked. About grep may be
you are right. At any event, in my job I never edit a file without saving a
separate copy of the original. Think that I'll never carry out a computation
(that may last weeks) without a raid 1 system. Not to mention certain
commercial OS that are carefully avoided for calculations (in my office for
everything), also because they need to emulate unix to do that.

cheers
francesco
--- Steven D'Aprano <[EMAIL PROTECTED]> wrote:

> On Mon, 07 Jan 2008 00:42:01 +, Grant Edwards wrote:
> 
> >> If you want to delete them, you still have to do the rest of the job
> >> yourself.
> > 
> > Nonsense.
> > 
> > How is this not doing what the OP asks?
> > 
> >grep -v pattern infile >outfile; mv outfile infile
> 
> It isn't deleting lines. As abstractions go, it comes pretty close, but 
> just try it on a disk with insufficient free space for the temporary 
> outfile and watch it break.
> 
> 
> -- 
> Steven
> -- 
> http://mail.python.org/mailman/listinfo/python-list
> 



  

Looking for last minute shopping deals?  
Find them fast with Yahoo! Search.  
http://tools.search.yahoo.com/newsearch/category.php?category=shopping
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Delete lines containing a specific word

2008-01-06 Thread Francesco Pietra

--- Steven D'Aprano <[EMAIL PROTECTED]> wrote:

> On Sun, 06 Jan 2008 13:33:52 -0800, Francesco Pietra wrote:
> 
> > Steven:
> > Thanks. See below please (of very marginal interest)
> > 
> > --- Steven D'Aprano <[EMAIL PROTECTED]> wrote:
> > 
> >> 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.
> > 
> > As I am at Debian Linux, I do that with grep -v
> 
> grep doesn't delete lines. grep matches lines. If you want to delete 
> them, you still have to do the rest of the job yourself.

Well, I use Debian Linux for scientific purposes, so that I have no much time
toget expertise even in the OS. Though, from the command (as user)

grep -v theword thefile.pdb 

I get thefile.pdb without the lines containing "theword".

> 
> 
> >> 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.
> > 
> > may be cumbersome, though I use  2>&1 | tee output file.pdb so that I
> > can see what happens on the screen and have the modified file.
> 
> Yes, matching lines and sending them to stdout is a better solution than 
> trying to delete them from a file.
> 
> 
> 
> -- 
> Steven
> -- 
> http://mail.python.org/mailman/listinfo/python-list
> 



  

Be a better friend, newshound, and 
know-it-all with Yahoo! Mobile.  Try it now.  
http://mobile.yahoo.com/;_ylt=Ahu06i62sR8HDtDypao8Wcj9tAcJ 

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


Re: Delete lines containing a specific word

2008-01-06 Thread Steven D'Aprano
On Mon, 07 Jan 2008 00:42:01 +, Grant Edwards wrote:

>> If you want to delete them, you still have to do the rest of the job
>> yourself.
> 
> Nonsense.
> 
> How is this not doing what the OP asks?
> 
>grep -v pattern infile >outfile; mv outfile infile

It isn't deleting lines. As abstractions go, it comes pretty close, but 
just try it on a disk with insufficient free space for the temporary 
outfile and watch it break.


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


Re: Delete lines containing a specific word

2008-01-06 Thread Steven D'Aprano
On Mon, 07 Jan 2008 00:33:36 +0100, Bjoern Schliessmann wrote:

> Steven D'Aprano wrote:
> 
>> grep doesn't delete lines. grep matches lines. If you want to delete
>> them, you still have to do the rest of the job yourself.
> 
> In which way does "grep -v mypattern myfile > myfile" not delete the
> lines matching mypattern?

Okay, that will delete the lines matching mypattern. Unfortunately it 
will also delete all the lines NOT matching mypattern as well. Try it for 
yourself -- just not on anything you care about.

This is what happens when abstractions leak. You *think* you're deleting 
lines, but you're not. That's just an abstraction, and when it leaks, you 
break things.



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


Re: Delete lines containing a specific word

2008-01-06 Thread Grant Edwards
On 2008-01-06, Steven D'Aprano <[EMAIL PROTECTED]> wrote:
> On Sun, 06 Jan 2008 13:33:52 -0800, Francesco Pietra wrote:
>
>> Steven:
>> Thanks. See below please (of very marginal interest)
>> 
>> --- Steven D'Aprano <[EMAIL PROTECTED]> wrote:
>> 
>>> 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.
>> 
>> As I am at Debian Linux, I do that with grep -v
>
> grep doesn't delete lines. grep matches lines.

grep does far more than that.

> If you want to delete them, you still have to do the rest of
> the job yourself.

Nonsense. 

How is this not doing what the OP asks?

   grep -v pattern infile >outfile; mv outfile infile

If you don't like explicitly using a second file, you can use
sed:

  sed -i '/pattern/d' filename

>>> 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.
>> 
>> may be cumbersome, though I use  2>&1 | tee output file.pdb so that I
>> can see what happens on the screen and have the modified file.
>
> Yes, matching lines and sending them to stdout is a better
> solution than trying to delete them from a file.

If you're matching all lines that don't contain the pattern in
question, then matching all lines and sending them to stdout
_is_ a way to delete them.

-- 
Grant Edwards   grante Yow! And furthermore,
  at   my bowling average is
   visi.comunimpeachable!!!
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Delete lines containing a specific word

2008-01-06 Thread bukzor
On Jan 6, 3:33 pm, Bjoern Schliessmann  wrote:
> Steven D'Aprano wrote:
> > grep doesn't delete lines. grep matches lines. If you want to
> > delete them, you still have to do the rest of the job yourself.
>
> In which way does "grep -v mypattern myfile > myfile" not delete the
> lines matching mypattern?
>
> Regards,
>
> Björn
>
> --
> BOFH excuse #184:
>
> loop found in loop in redundant loopback

If you do that, you'll find an empty file at the end.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Delete lines containing a specific word

2008-01-06 Thread Bjoern Schliessmann
Steven D'Aprano wrote:

> grep doesn't delete lines. grep matches lines. If you want to
> delete them, you still have to do the rest of the job yourself.

In which way does "grep -v mypattern myfile > myfile" not delete the
lines matching mypattern?

Regards,


Björn

-- 
BOFH excuse #184:

loop found in loop in redundant loopback

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


Re: Delete lines containing a specific word

2008-01-06 Thread Steven D'Aprano
On Sun, 06 Jan 2008 13:33:52 -0800, Francesco Pietra wrote:

> Steven:
> Thanks. See below please (of very marginal interest)
> 
> --- Steven D'Aprano <[EMAIL PROTECTED]> wrote:
> 
>> 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.
> 
> As I am at Debian Linux, I do that with grep -v

grep doesn't delete lines. grep matches lines. If you want to delete 
them, you still have to do the rest of the job yourself.


>> 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.
> 
> may be cumbersome, though I use  2>&1 | tee output file.pdb so that I
> can see what happens on the screen and have the modified file.

Yes, matching lines and sending them to stdout is a better solution than 
trying to delete them from a file.



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


Re: Delete lines containing a specific word

2008-01-06 Thread Martin Marcher
On Sunday 06 January 2008 21:25 Francesco Pietra wrote:
>> yes lines starting with a "#" are comments in python but that shouldn't
>> be of concern for your input data. I don't quite get what you want
>> here...
> 
> Leaving the lines commented out would permit to resume them or at least
> remeber what was suppressed. During trial and error set up of a
> calculation one never knows exactly the outcome

Ah I get it.

To clarify:
The character python uses as a comment has nothing to do with the character
your data file uses as a comment.

So you could of course use the "#" sign (which makes sense)
You could also use "//" (C-Style) or whatever you like

class CommentHelper(object):
"""Provides the necessary
methods to comment or uncomment a line of text.
"""
# Yes I know this docstring is badly formatted but
# I thought it's nicer to keep the indentation.
def __init__(self, commentStr=None):
if commentStr:
self.commentStr = commentStr
else:
self.commentStr = "MY_SUPER_COMMENT_STRING"
def commentLine(line):
"""Comments a line with the
initialized comment string.
"""
return self.commentStr + " " + line
def uncommentLine(line):
"""Uncomments a line iff it is
preceded by the comment string.
"""
if line.startsWith(self.commentStr):
return line[len(self.commentStr)].lstrip()
raise Exception("Can't uncomment Line with no comment")


You want to read up about:
* (new style) classes
* parameters with default values
* docstrings
* Exceptions


That code is untested and may contain errors. I'll let the debugging be your
task :)

hope it points you to the right topics to read up about:
http://docs.python.org/
http://docs.python.org/tut/tut.html
http://www.diveintopython.org/

martin

-- 
http://noneisyours.marcher.name
http://feeds.feedburner.com/NoneIsYours

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


Re: Delete lines containing a specific word

2008-01-06 Thread Francesco Pietra
Steven:
Thanks. See below please (of very marginal interest)

--- Steven D'Aprano <[EMAIL PROTECTED]> wrote:

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

As I am at Debian Linux, I do that with grep -v


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

may be cumbersome, though I use  2>&1 | tee output file.pdb so that I can see
what happens on the screen and have the modified 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
> 
> 
> -- 
> http://mail.python.org/mailman/listinfo/python-list
> 



  

Be a better friend, newshound, and 
know-it-all with Yahoo! Mobile.  Try it now.  
http://mobile.yahoo.com/;_ylt=Ahu06i62sR8HDtDypao8Wcj9tAcJ 

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


Re: Delete lines containing a specific word

2008-01-06 Thread Steven D'Aprano
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


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


Re: Delete lines containing a specific word

2008-01-06 Thread Francesco Pietra

--- Martin Marcher <[EMAIL PROTECTED]> wrote:

> On Sunday 06 January 2008 18:21 Francesco Pietra 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()
> 
> >>> import re
> >>> s = ["hello", "world", "", "\t ", " ", "#asdfasdf"]
> >>> pattern = re.compile("^\s*$")
> >>> #only "hello" should be printed as "world" is a word we want to skip
> >>> for line in s:
> ... if "world" in line:
> ... continue
> ... if pattern.match(line):
> ... continue
> ... if line.startswith("#"):
> ... continue
> ... print line
> ...
> hello
> >>>
> 
> you have to adapt it to be able to match more than a single word
> 
> > 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 #.
> 
> yes lines starting with a "#" are comments in python but that shouldn't be
> of concern for your input data. I don't quite get what you want here...

Leaving the lines commented out would permit to resume them or at least remeber
what was suppressed. During trial and error set up of a calculation one never
knows exactly the outcome
thanks
francesco
> 
> hth
> martin
> 
> -- 
> http://noneisyours.marcher.name
> http://feeds.feedburner.com/NoneIsYours
> 
> -- 
> http://mail.python.org/mailman/listinfo/python-list
> 



  

Looking for last minute shopping deals?  
Find them fast with Yahoo! Search.  
http://tools.search.yahoo.com/newsearch/category.php?category=shopping
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Delete lines containing a specific word

2008-01-06 Thread Martin Marcher
On Sunday 06 January 2008 18:21 Francesco Pietra 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()

>>> import re
>>> s = ["hello", "world", "", "\t ", " ", "#asdfasdf"]
>>> pattern = re.compile("^\s*$")
>>> #only "hello" should be printed as "world" is a word we want to skip
>>> for line in s:
... if "world" in line:
... continue
... if pattern.match(line):
... continue
... if line.startswith("#"):
... continue
... print line
...
hello
>>>

you have to adapt it to be able to match more than a single word

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

yes lines starting with a "#" are comments in python but that shouldn't be
of concern for your input data. I don't quite get what you want here...

hth
martin

-- 
http://noneisyours.marcher.name
http://feeds.feedburner.com/NoneIsYours

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


Re: Delete lines containing a specific word

2008-01-06 Thread Grant Edwards
On 2008-01-06, Matt Nordhoff <[EMAIL PROTECTED]> wrote:

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

> If you're on Linux, why not just use grep?
>
> $ grep -v theword output.pdb

And if you're on Windows, install Cygwin, and then use grep. Or
install any of about a half-dozen native Win23 implementations
of grep.

-- 
Grant Edwards   grante Yow! I'm having a BIG BANG
  at   THEORY!!
   visi.com
-- 
http://mail.python.org/mailman/listinfo/python-list


Fwd: Delete lines containing a specific word

2008-01-06 Thread Francesco Pietra
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   446  27.436  34.037  56.145  1.00  0.00   H
HETATM 7009  H2  WAT   446  27.049  33.827  54.563  1.00  0.00   H

occurring in a 300MB file.

In present case each three-lines block is followed by line renumbering (7007,
7008, 7009 are line numbers). So, the script is simply to strip the lines
containing :WAT".

In other cases the block is intermediate. Therefore, a second script would be
useful where the line that follow the block are renumbered, e.g, line "HETATM
7010" following the above block renumbered "HEATM 7006".

thanks
francesco


--- Francesco Pietra <[EMAIL PROTECTED]> wrote:

> Date: Sun, 6 Jan 2008 09:21:33 -0800 (PST)
> From: Francesco Pietra <[EMAIL PROTECTED]>
> Subject: Delete lines containing a specific word
> To: python-list@python.org
> 
> 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/newsearch/category.php?category=shopping
> 



  

Looking for last minute shopping deals?  
Find them fast with Yahoo! Search.  
http://tools.search.yahoo.com/newsearch/category.php?category=shopping
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Delete lines containing a specific word

2008-01-06 Thread Matt Nordhoff
Francesco Pietra 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

If you're on Linux, why not just use grep?

$ grep -v theword output.pdb

Otherwise, just replace "if line:" with "if 'theword' not in line:".

Thanks to rstrip and print, the script changes the file's line endings
to the platform's default one. "rstrip()" also removes other types of
whitespace, like spaces and tabs.

I'm not sure how that script handles different line endings in the file.
Might want to use 'rb' instead of 'r' to open the file in binary mode on
Windows (to also help with other issues), or 'rU' or 'rbU' to use
Python's universal newline mode.)
-- 
-- 
http://mail.python.org/mailman/listinfo/python-list


Delete lines containing a specific word

2008-01-06 Thread Francesco Pietra
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/newsearch/category.php?category=shopping
-- 
http://mail.python.org/mailman/listinfo/python-list