On Jan 26, 5:08 pm, Gary Herron <[EMAIL PROTECTED]> wrote: > Frank Potter wrote: > > I only want to remove the comments which begin with "//". > > I did like this, but it doesn't work. > > > r=re.compile(ur"//[^\r\n]+$", re.UNICODE|re.VERBOSE) > > f=file.open("mycpp.cpp","r") > > f=unicode(f,"utf8") > > r.sub(ur"",f) > > > Will somebody show me the right way? > > Thanks~~If you expect help with a problem, it would be nice if you told us > > what > the problem is. What error did you get? > > But even without that I see lots of errors: > > You must import re before you use it: > import re > > Open a file with open((..) not file.open(...). > > Once you open the file you must *read* the contents and operate on that: > data = f.read() > > Then you ought to close the file: > f.close() > > Now you can do your sub on the string in data -- but note, THIS WON'T > CHANGE data, but rather returns a new string which you must assign to > something: > > new_data = r.sub(ur"", data) > > Then do something with the new string. > > Also I fear your regular expression is incorrect. > > Cheers, > Gary Herron
Thank you. I'm very sorry because I was in a hurry when I post this thread. I'll post again my code here: [CODE] import re f=open("show_btchina.user.js","r").read() f=unicode(f,"utf8") r=re.compile(ur"//[^\r\n]+$", re.UNICODE|re.VERBOSE) f_new=r.sub(ur"",f) open("modified.js","w").write(f_new.encode("utf8")) [/CODE] And, the problem is, it seems that only the last comment is removed. How can I remove all of the comments, please? -- http://mail.python.org/mailman/listinfo/python-list