python regex to remove comments

后端 未结 3 2033
野趣味
野趣味 2020-12-06 15:27

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

3条回答
  •  不知归路
    2020-12-06 15:55

    sed -e '1,2p' -e '/^\s*#/d' infile
    

    Then wrap this in a subprocess.Popen call.

    However, this doesn't substitute a real parser! Why would this be of interest? Well, assume this Python script:

    output = """
    This is
    #1 of 100"""
    

    Boom, any non-parsing solution instantly breaks your script.

提交回复
热议问题