python 脚本去除#开头和空行
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了python 脚本去除#开头和空行相关的知识,希望对你有一定的参考价值。
#!/usr/bin/python
# -*- coding: UTF-8 -*-
# python 3.4
import re
import sys
def main(argv):
rf = open(argv[0],‘rU‘,encoding=‘UTF-8‘)
outstring = ‘‘
try:
outstring = rf.read()
finally:
rf.close()
m = re.compile(r‘#.*‘)
outtmp = re.sub(m,‘‘,outstring)
outstring = outtmp
outtmp = re.sub(r‘^\n|\n+(?=\n)|\n$‘,‘‘,outstring)
outstring = outtmp
print(argv[0], argv[1])
f = open(argv[1], ‘w‘)
f.write(outstring)
f.close()
if __name__ == "__main__":
main(sys.argv[1:])
以上是关于python 脚本去除#开头和空行的主要内容,如果未能解决你的问题,请参考以下文章