Advertisement
cd62131

remove comment in python

May 16th, 2019
532
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.75 KB | None | 0 0
  1. #a
  2. #b
  3. import sys  #c
  4. from tokenize import tokenize, untokenize, COMMENT, NEWLINE, NL
  5. from io import BytesIO
  6. ret = []  #d
  7. with open(sys.argv[1]) as f:
  8.     for line in f:
  9.         in_comment = False  #e
  10.         for tok in tokenize(BytesIO(line.encode('utf-8')).readline):
  11.             if tok.type == COMMENT:
  12.                 in_comment = True
  13.                 continue
  14.             elif in_comment and tok.type == NL:
  15.                 in_comment = False
  16.                 continue
  17.             elif in_comment and tok.type == NEWLINE:
  18.                 in_comment = False
  19.                 ret.append((tok.type, tok.string))
  20.                 continue
  21.             else:  #f
  22.                 ret.append((tok.type, tok.string))
  23. print(untokenize(ret).decode('utf-8'))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement