how to remove c++ comments from a cpp file?

Laurent Rahuel lrahuel.notgood at
Fri Jan 26 16:23:39 CET 2007

And using the codecs module

import codecs

f ="show_btchina.user.js","r","utf-8")
modf ="modified.js","w","utf-8")
for line in f:
     idx = line.find(u"//")
     if idx==0:
     elif idx>0:
         line = line[:idx]+u'\n'

Gabriel Genellina wrote:

> At Friday 26/1/2007 06:54, Frank Potter wrote:
>>import re
>>r=re.compile(ur"//[^\r\n]+$", re.UNICODE|re.VERBOSE)
>>And, the problem is, it seems that only the last comment is removed.
>>How can I remove all of the comments, please?
> Note that it's not as easy as simply deleting from // to end of line,
> because those characters might be inside a string literal. But if you
> can afford the risk, this is a simple way without re:
> f = open("show_btchina.user.js","r")
> modf = open("modified.js","w")
> for line in f:
>      uline=unicode(line,"utf8")
>      idx = uline.find("//")
>      if idx==0:
>          continue
>      elif idx>0:
>          uline = uline[:idx]+'\n'
>      modf.write(uline.encode("utf8"))
> modf.close()
> f.close()

More information about the Python-list mailing list