import os
import sys
def parse_log(fd):
i = 0
line = fd.readline()
while line:
line = line.strip('\n\r ')
if line.startswith("---"):
i += 1
if i == 3:
i = 1
sys.stdout.write('\n')
line = fd.readline()
continue
sys.stdout.write(line)
line = fd.readline()
if __name__ == "__main__":
fd = open(sys.argv[1], "r")
parse_log(fd)
fd.close()
Log文件示例:
---
File:
log.txt
---
memcpy
memset
---
File:
log2.c
---
strcpy
strcat
---
File:
log3.lg
---
strcmp
memmove
---
---
---
最后结果是: