removing comments form a file

wittempj at hotmail.com wittempj at hotmail.com
Mon Jan 3 10:22:14 EST 2005


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 at 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 at 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 at linux:~/programmeren/python/rmcomment$ cat newexample.txt
> Hello Fuckin' World
> But this one not! #Even not this comment
> jwaixs at 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 at 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 at 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 at 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 at 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.




More information about the Python-list mailing list