How would I write a regex that removes all comments that start with the # and stop at the end of the line — but at the same time exclude the first two lines which say
#!/usr/bin/python
and
#-*- coding: utf-8 -*-
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
You can remove comments by parsing the Python code with
tokenize.generate_tokens. The following is a slightly modified version of this example from the docs:For example:
If script.py contains
then the output of
nocommentis