removing comments form a file

Discussion in 'Python' started by Noud Aldenhoven, Jan 3, 2005.

  1. Hello everyone,

    I was wondering how to remove comments away form a file.
    So that's why I made this script.

    ===============================
    #!/usr/bin/env python

    import sys
    import string
    import time

    helptext = "usage: python rmcomment [oldfile] [newfile] [comment]"

    def rmcomment(oldfile, newfile, comment):
    oldfile = open(oldfile, 'r')
    newfile = open(newfile, 'w')
    ccount = 0
    lcount = 0
    for line in oldfile.readlines():
    splitline = string.split(line)
    pstest = 0
    fileline = ""
    for word in splitline:
    if word[:2] == comment:
    pstest = -1
    ccount += 1
    pass
    elif pstest == -1:
    pass
    else:
    fileline += word + " "
    if len(fileline) == 0:
    pass
    else:
    newfile.write(fileline + "\n")
    lcount += 1
    print "Done... in %s seconds\nRemoved comment from %s lines\nWrote %
    lines to %s" % (time.time()-start , ccount, lcount, newfile)
    raw_input("Push the enter button to quit>")

    if __name__ == "__main__":
    if sys.argv[1] == "-h" or sys.argv[1] == "-help":
    print helptext
    else:
    start = time.time()
    oldfile = sys.argv[1]
    newfile = sys.argv[2]
    comment = sys.argv[3]
    rmcomment(oldfile, newfile, comment)


    ========================================

    This script works fine with standard text files. An example is this one:

    example.txt:

    Hello Fuckin' World //how are you doing today
    //I think it delete this sentence and the next sentence too!

    But this one not! #Even not this comment

    end example.txt

    If I use my script, named rmcomment.py I get this:

    jwaixs@linux:~/programmeren/python/rmcomment$ cat example.txt
    Hello Fuckin' World //how are you doing today
    //I think it delete this sentence and the next sentence too!

    But this one not! #Even not this comment's
    jwaixs@linux:~/programmeren/python/rmcomment$ python rmcomment.py
    example.txt newexample.txt //
    Done... in 0.00104999542236 seconds
    Removed comment from 2 lines
    Wrote 2nes to <open file 'newexample.txt', mode 'w' at 0x403e1c60>
    Push the enter button to quit>
    jwaixs@linux:~/programmeren/python/rmcomment$ cat newexample.txt
    Hello Fuckin' World
    But this one not! #Even not this comment
    jwaixs@linux:~/programmeren/python/rmcomment$

    works fine... but here's my problem. If I use rmcomment.py also the
    whitelines will be removed. And I don't want that to happen. Here's another
    example:

    jwaixs@linux:~/programmeren/python/rmcomment$ cat otherexample.txt
    //This shows what whitelines are doing here
    left from me is a nice white line tabs
    and here left are at least 3 white line tabs

    //and ofcourse, comments will be deleted
    jwaixs@linux:~/programmeren/python/rmcomment$ python rmcomment.py
    otherexample.txt newotherexample.txt //
    Done... in 0.0011351108551 seconds
    Removed comment form 2 lines
    Wrote 2nes to <open file 'newotherexample.txt', mode 'w' at 0x403e1c60>
    Push the enter button to quit>
    jwaixs@linux:~/programmeren/python/rmcomment$ cat newotherexample.txt
    left from me is a nice white line tabs
    and here left are at least 3 white line tabs
    jwaixs@linux:~/programmeren/python/rmcomment$

    My beautiful whitelines are gone! And I don't want that!
    I've thaught how to fix this for a time, but I can't make it on my own. Too
    less programming experiance, I'm afraid.
    Could someone help me with this problem? Or fix the script or give a hint or
    something?

    Thank you at least for reading this post,

    Noud Aldenhoven
    The Netherlands (In de beurt bij Nijmegen, voor de nieuwschierigen)

    ps. Yes, I'm a Dyslextion and can't write correct english. I'm sorry for
    that.
     
    Noud Aldenhoven, Jan 3, 2005
    #1
    1. Advertising

  2. Ah,

    Thank you, I never thaught about the re module. Now I can do some cool stuf.

    Greetings,
    Noud Aldenhoven

    ps. Zal ik een keer langs komen? ;-P

    wrote:

    > You cold do something like this:
    >
    >>>> import re
    >>>> commentpattern = re.compile('.*(?=//)|.*(?!//)')
    >>>> stringwithcomment = 'Blah di blah // some comment'
    >>>> match = commentpattern.match(stringwithcomment)
    >>>> match.group()

    > 'Blah di blah '
    >>>> stringwithoutcomment = 'Blah di blah'
    >>>> match = commentpattern.match(stringwithoutcomment)
    >>>> match.group()

    > 'Blah di blah'
    >>>> blankline = '\n'
    >>>> match = commentpattern.match(blankline)
    >>>> match.group()

    > ''
    >>>>

    >
    > and put this in a loop where you iterate over your file.
    > Martin (The Netherlands, Amsterdam, bij Diemen)
    >
    > Noud Aldenhoven wrote:
    >> Hello everyone,
    >>
    >> I was wondering how to remove comments away form a file.
    >> So that's why I made this script.
    >>
    >> ===============================
    >> #!/usr/bin/env python
    >>
    >> import sys
    >> import string
    >> import time
    >>
    >> helptext = "usage: python rmcomment [oldfile] [newfile] [comment]"
    >>
    >> def rmcomment(oldfile, newfile, comment):
    >> oldfile = open(oldfile, 'r')
    >> newfile = open(newfile, 'w')
    >> ccount = 0
    >> lcount = 0
    >> for line in oldfile.readlines():
    >> splitline = string.split(line)
    >> pstest = 0
    >> fileline = ""
    >> for word in splitline:
    >> if word[:2] == comment:
    >> pstest = -1
    >> ccount += 1
    >> pass
    >> elif pstest == -1:
    >> pass
    >> else:
    >> fileline += word + " "
    >> if len(fileline) == 0:
    >> pass
    >> else:
    >> newfile.write(fileline + "\n")
    >> lcount += 1
    >> print "Done... in %s seconds\nRemoved comment from %s

    > lines\nWrote %
    >> lines to %s" % (time.time()-start , ccount, lcount, newfile)
    >> raw_input("Push the enter button to quit>")
    >>
    >> if __name__ == "__main__":
    >> if sys.argv[1] == "-h" or sys.argv[1] == "-help":
    >> print helptext
    >> else:
    >> start = time.time()
    >> oldfile = sys.argv[1]
    >> newfile = sys.argv[2]
    >> comment = sys.argv[3]
    >> rmcomment(oldfile, newfile, comment)
    >>
    >>
    >> ========================================
    >>
    >> This script works fine with standard text files. An example is this

    > one:
    >>
    >> example.txt:
    >>
    >> Hello Fuckin' World //how are you doing today
    >> //I think it delete this sentence and the next sentence too!
    >>
    >> But this one not! #Even not this comment
    >>
    >> end example.txt
    >>
    >> If I use my script, named rmcomment.py I get this:
    >>
    >> jwaixs@linux:~/programmeren/python/rmcomment$ cat example.txt
    >> Hello Fuckin' World //how are you doing today
    >> //I think it delete this sentence and the next sentence too!
    >>
    >> But this one not! #Even not this comment's
    >> jwaixs@linux:~/programmeren/python/rmcomment$ python rmcomment.py
    >> example.txt newexample.txt //
    >> Done... in 0.00104999542236 seconds
    >> Removed comment from 2 lines
    >> Wrote 2nes to <open file 'newexample.txt', mode 'w' at 0x403e1c60>
    >> Push the enter button to quit>
    >> jwaixs@linux:~/programmeren/python/rmcomment$ cat newexample.txt
    >> Hello Fuckin' World
    >> But this one not! #Even not this comment
    >> jwaixs@linux:~/programmeren/python/rmcomment$
    >>
    >> works fine... but here's my problem. If I use rmcomment.py also the
    >> whitelines will be removed. And I don't want that to happen. Here's

    > another
    >> example:
    >>
    >> jwaixs@linux:~/programmeren/python/rmcomment$ cat otherexample.txt
    >> //This shows what whitelines are doing here
    >> left from me is a nice white line tabs
    >> and here left are at least 3 white line tabs
    >>
    >> //and ofcourse, comments will be deleted
    >> jwaixs@linux:~/programmeren/python/rmcomment$ python rmcomment.py
    >> otherexample.txt newotherexample.txt //
    >> Done... in 0.0011351108551 seconds
    >> Removed comment form 2 lines
    >> Wrote 2nes to <open file 'newotherexample.txt', mode 'w' at

    > 0x403e1c60>
    >> Push the enter button to quit>
    >> jwaixs@linux:~/programmeren/python/rmcomment$ cat newotherexample.txt
    >> left from me is a nice white line tabs
    >> and here left are at least 3 white line tabs
    >> jwaixs@linux:~/programmeren/python/rmcomment$
    >>
    >> My beautiful whitelines are gone! And I don't want that!
    >> I've thaught how to fix this for a time, but I can't make it on my

    > own. Too
    >> less programming experiance, I'm afraid.
    >> Could someone help me with this problem? Or fix the script or give a

    > hint or
    >> something?
    >>
    >> Thank you at least for reading this post,
    >>
    >> Noud Aldenhoven
    >> The Netherlands (In de beurt bij Nijmegen, voor de nieuwschierigen)
    >>
    >> ps. Yes, I'm a Dyslextion and can't write correct english. I'm sorry

    > for
    >> that.
     
    Noud Aldenhoven, Jan 3, 2005
    #2
    1. Advertising

  3. Noud Aldenhoven

    Guest

    You cold do something like this:

    >>> import re
    >>> commentpattern = re.compile('.*(?=//)|.*(?!//)')
    >>> stringwithcomment = 'Blah di blah // some comment'
    >>> match = commentpattern.match(stringwithcomment)
    >>> match.group()

    'Blah di blah '
    >>> stringwithoutcomment = 'Blah di blah'
    >>> match = commentpattern.match(stringwithoutcomment)
    >>> match.group()

    'Blah di blah'
    >>> blankline = '\n'
    >>> match = commentpattern.match(blankline)
    >>> match.group()

    ''
    >>>


    and put this in a loop where you iterate over your file.
    Martin (The Netherlands, Amsterdam, bij Diemen)

    Noud Aldenhoven wrote:
    > Hello everyone,
    >
    > I was wondering how to remove comments away form a file.
    > So that's why I made this script.
    >
    > ===============================
    > #!/usr/bin/env python
    >
    > import sys
    > import string
    > import time
    >
    > helptext = "usage: python rmcomment [oldfile] [newfile] [comment]"
    >
    > def rmcomment(oldfile, newfile, comment):
    > oldfile = open(oldfile, 'r')
    > newfile = open(newfile, 'w')
    > ccount = 0
    > lcount = 0
    > for line in oldfile.readlines():
    > splitline = string.split(line)
    > pstest = 0
    > fileline = ""
    > for word in splitline:
    > if word[:2] == comment:
    > pstest = -1
    > ccount += 1
    > pass
    > elif pstest == -1:
    > pass
    > else:
    > fileline += word + " "
    > if len(fileline) == 0:
    > pass
    > else:
    > newfile.write(fileline + "\n")
    > lcount += 1
    > print "Done... in %s seconds\nRemoved comment from %s

    lines\nWrote %
    > lines to %s" % (time.time()-start , ccount, lcount, newfile)
    > raw_input("Push the enter button to quit>")
    >
    > if __name__ == "__main__":
    > if sys.argv[1] == "-h" or sys.argv[1] == "-help":
    > print helptext
    > else:
    > start = time.time()
    > oldfile = sys.argv[1]
    > newfile = sys.argv[2]
    > comment = sys.argv[3]
    > rmcomment(oldfile, newfile, comment)
    >
    >
    > ========================================
    >
    > This script works fine with standard text files. An example is this

    one:
    >
    > example.txt:
    >
    > Hello Fuckin' World //how are you doing today
    > //I think it delete this sentence and the next sentence too!
    >
    > But this one not! #Even not this comment
    >
    > end example.txt
    >
    > If I use my script, named rmcomment.py I get this:
    >
    > jwaixs@linux:~/programmeren/python/rmcomment$ cat example.txt
    > Hello Fuckin' World //how are you doing today
    > //I think it delete this sentence and the next sentence too!
    >
    > But this one not! #Even not this comment's
    > jwaixs@linux:~/programmeren/python/rmcomment$ python rmcomment.py
    > example.txt newexample.txt //
    > Done... in 0.00104999542236 seconds
    > Removed comment from 2 lines
    > Wrote 2nes to <open file 'newexample.txt', mode 'w' at 0x403e1c60>
    > Push the enter button to quit>
    > jwaixs@linux:~/programmeren/python/rmcomment$ cat newexample.txt
    > Hello Fuckin' World
    > But this one not! #Even not this comment
    > jwaixs@linux:~/programmeren/python/rmcomment$
    >
    > works fine... but here's my problem. If I use rmcomment.py also the
    > whitelines will be removed. And I don't want that to happen. Here's

    another
    > example:
    >
    > jwaixs@linux:~/programmeren/python/rmcomment$ cat otherexample.txt
    > //This shows what whitelines are doing here
    > left from me is a nice white line tabs
    > and here left are at least 3 white line tabs
    >
    > //and ofcourse, comments will be deleted
    > jwaixs@linux:~/programmeren/python/rmcomment$ python rmcomment.py
    > otherexample.txt newotherexample.txt //
    > Done... in 0.0011351108551 seconds
    > Removed comment form 2 lines
    > Wrote 2nes to <open file 'newotherexample.txt', mode 'w' at

    0x403e1c60>
    > Push the enter button to quit>
    > jwaixs@linux:~/programmeren/python/rmcomment$ cat newotherexample.txt
    > left from me is a nice white line tabs
    > and here left are at least 3 white line tabs
    > jwaixs@linux:~/programmeren/python/rmcomment$
    >
    > My beautiful whitelines are gone! And I don't want that!
    > I've thaught how to fix this for a time, but I can't make it on my

    own. Too
    > less programming experiance, I'm afraid.
    > Could someone help me with this problem? Or fix the script or give a

    hint or
    > something?
    >
    > Thank you at least for reading this post,
    >
    > Noud Aldenhoven
    > The Netherlands (In de beurt bij Nijmegen, voor de nieuwschierigen)
    >
    > ps. Yes, I'm a Dyslextion and can't write correct english. I'm sorry

    for
    > that.
     
    , Jan 3, 2005
    #3
    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. Replies:
    2
    Views:
    1,222
    Tom Regner
    Jul 15, 2004
  2. Replies:
    0
    Views:
    1,157
  3. Monk
    Replies:
    10
    Views:
    1,502
    Michael Wojcik
    Apr 20, 2005
  4. Robhy B.
    Replies:
    3
    Views:
    114
    zuerrong
    Dec 9, 2010
  5. Replies:
    4
    Views:
    634
    Dr John Stockton
    Jun 3, 2006
Loading...

Share This Page