python中去掉文件的注释

发布时间:2019-09-08 09:13:06编辑:auto阅读(1903)

    import sys

    import re

    PY_PATTERN = re.compile(

        r"""

         \s*\#(?:[^\r\n])*

         | \s*__(?:[^\r\n]*)

         | "{3}(?:\\.|[^\\])*"{3}

         | '{3}(?:\\.|[^\\])*'{3}

         """,

        re.VERBOSE | re.MULTILINE | re.DOTALL

    )

     

    txt = open("cmd.txt").readlines()

    b = re.sub(PY_PATTERN,'', ''.join(txt))

    single = re.compile(r"\n\n")

    b = re.sub(single,'\n',b)

    print(b)


关键字