美文网首页
Python脚本删除注释行

Python脚本删除注释行

作者: YuQiao0303 | 来源:发表于2020-04-26 15:59 被阅读0次

2020-04-26
目前还没写删除多余空行的代码
(可以用word,查找替换,将pp替换为^p)


import re
# 1.首先是读取文件
rf = open('code.txt', 'r', encoding='UTF-8')
outFile =open("dllNew.txt","w")
outstring = ''
try:
    outstring = rf.read()
finally:
    rf.close()
# outstring = "test//666\n//这是注释\n/*这也是注释*/"
# 2.清除块级注释
m = re.compile(r'/[*].*?[*]/', re.S)
outtmp = re.sub(m,'',outstring)
outstring = outtmp
# 3.清除 // 行级注释
m = re.compile(r'//.*')
outtmp = re.sub(m, '', outstring)
outstring = outtmp


outFile.write(outstring)
print(outstring)

outFile.close()

相关文章

网友评论

      本文标题:Python脚本删除注释行

      本文链接:https://www.haomeiwen.com/subject/xecjwhtx.html