python中去掉文件的注释

2020-01-08 16:51:26 浏览数 (3)

import sys

import re

PY_PATTERN = re.compile(

    r"""

     s*#(?:[^rn])*

     | s*__(?:[^rn]*)

     | "{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"nn")

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

print(b)

0 人点赞