0

I really want to remove all comments like: #This does this for example, as I annotated it ages ago, and I want to try and work out what all the parts of the code do, without going through 600+ lines and deleting them individually.

Terry Jan Reedy
  • 18,414
  • 3
  • 40
  • 52
18166
  • 111
  • 1
  • 13

1 Answers1

0

For this, you could use re module.

>>> s = """foo bar
#This does this
 # foo bar
bar foo"""
>>> for i in s.split('\n'):
        if not re.match(r'\s*#', i):
            print(i)


foo bar
bar foo
Avinash Raj
  • 172,303
  • 28
  • 230
  • 274
  • Probably better to use `\s*` before the `#`, in case the OP (or some future visitor) uses tabs. Also, note that this will struggle with e.g. multiline strings *within* the code. – jonrsharpe Feb 10 '15 at 16:18
  • since we are splitting the input according to the `\n` chaarcter, `\s*` at the start won't create any problems. – Avinash Raj Feb 10 '15 at 16:25
  • Thanks for the answer, but this doesn't answer my question - I meant how to remove them in the script editor, which I thought was called IDLE. I will edit my question for this. – 18166 Feb 10 '15 at 17:37