removing comments form a file

  • Thread starter Noud Aldenhoven
  • Start date
N

Noud Aldenhoven

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

Noud Aldenhoven

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

You cold do something like this:

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

Noud said:
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.
 
W

wittempj

You cold do something like this:

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

Noud said:
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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,764
Messages
2,569,564
Members
45,039
Latest member
CasimiraVa

Latest Threads

Top