473,412 Members | 5,361 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,412 software developers and data experts.

removing comments form a file

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.


Jul 18 '05 #1
2 2606
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.


Jul 18 '05 #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

wi******@hotmail.com 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.


Jul 18 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

16
by: qwweeeit | last post by:
In analysing a very big application (pysol) made of almost 100 sources, I had the need to remove comments. Removing the comments which take all the line is straightforward... Instead for the...
2
by: user | last post by:
Hi How do i remove comments from a DOM object ? I tried searching the man pages XML::DOM, XML::DOM::Comment and XML::DOM::Document but could not find anything The code used to parse the...
16
by: graham.reeds | last post by:
I am updating a website that uses a countdown script embedded on the page. When the page is served the var's are set to how long the countdown has left in minutes and seconds, but the rest of the...
39
by: Timex | last post by:
I want to delete all comments in .c file. Size of .c file is very big. Any good idea to do this? Please show me example code.
8
by: darkknight | last post by:
Hi Is there any commonest practice or "industry standard" regarding comments that appear at the head of a function definition or function prototype? Do they go in the .c file only, the .h...
3
by: Jeremy Owens-Boggs | last post by:
We are trying to implement a dual list box selection where you have two list boxes, You highlight items in the right side list box, click a button and this moves those items over to the left hand...
6
by: Nathan Sokalski | last post by:
I recently converted some ASP.NET 1.1 projects of mine, created with Visual Studio .NET 2003, to Web Application Projects in Visual Studio .NET 2005 so that I could use ASP.NET 2.0 (All my ASP.NET...
16
by: junky_fellow | last post by:
Is there any efficcient way of removing the newline character from the buffer read by fgets() ? Is there any library function that is similar to fgets() but also tells how many bytes it read...
2
by: beatTheDevil | last post by:
Hey guys, As the title says I'm trying to make a regular expression (regex/regexp) for use in removing the comments from code. In this case, this particular regex is meant to match /* ... */...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.