#! /usr/bin/env python# encoding:utf-8# ! /usr/bin/env python# encoding:utf-8import reimport linecachefile = 'D:/PUSCH_job3.txt'outfile = 'D:/outfile3.txt'lineNumber = 1with open(file,'r') as f: number = [] for line in f.readlines(): m = re.findall(r"UINT32 O_/w+",line) #匹配含有字符'UINT32 O_'的行 if m: number.append(lineNumber) n = re.findall(r"OUT /w+",line) #假设只有一个OUT if n: number.append(lineNumber) lineNumber += 1 with open(outfile, 'w+') as f_w: for j in range(len(number)): if j%2 == 0: start = number[j] end = number[j+1] destlines = linecache.getlines(file)[start-1:end] #截取start-end行的字符,不包括start-1,但包含end行 f_w.write('extern ') for i in range(len(destlines)): if i != len(destlines)-1: f_w.write(destlines[i]) else: f_w.write(destlines[i].replace('/n',';/n')) f_w.write('/n')