天天看點

python中去掉檔案的注釋

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)

本文轉自 帥楓小明 51CTO部落格,原文連結:http://blog.51cto.com/576642026/1774712,如需轉載請自行聯系原作者