Delete lines containing a specific word

Discussion in 'Python' started by Francesco Pietra, Jan 6, 2008.

  1. 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
    Francesco Pietra, Jan 6, 2008
    #1
    1. Advertising

  2. 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
    Steven D'Aprano, Jan 6, 2008
    #2
    1. Advertising

  3. OS memories (was: Delete lines containing a specific word

    On Sun, 06 Jan 2008 21:09:41 -0000, Steven D'Aprano
    <> 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://wlfraed.home.netcom.com/
    (Bestiaria Support Staff: )
    HTTP://www.bestiaria.com/
    Dennis Lee Bieber, Jan 6, 2008
    #3
  4. Francesco Pietra

    Guest

    On Jan 7, 1:21 am, 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
    >
    > ____________________________________________________________________________________
    > 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
    , Jan 7, 2008
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Murali
    Replies:
    2
    Views:
    544
    Jerry Coffin
    Mar 9, 2006
  2. Matt Nordhoff

    Re: Delete lines containing a specific word

    Matt Nordhoff, Jan 6, 2008, in forum: Python
    Replies:
    1
    Views:
    323
    Grant Edwards
    Jan 6, 2008
  3. Francesco Pietra

    Fwd: Delete lines containing a specific word

    Francesco Pietra, Jan 6, 2008, in forum: Python
    Replies:
    0
    Views:
    249
    Francesco Pietra
    Jan 6, 2008
  4. Francesco Pietra

    Re: Delete lines containing a specific word

    Francesco Pietra, Jan 6, 2008, in forum: Python
    Replies:
    6
    Views:
    273
    Steven D'Aprano
    Jan 7, 2008
  5. Eden Cardim
    Replies:
    11
    Views:
    192
    Eden Cardim
    Mar 13, 2006
Loading...

Share This Page