import collectionsimport itertoolsimport syswith open('huge-file') as f: before = collections.deque(maxlen=10) for line in f: if 'word' in line: sys.stdout.writelines(before) sys.stdout.write(line) sys.stdout.writelines(itertools.islice(f, 10)) break before.append(line)
用于
collections.deque在比赛前保存多达10行,并在比赛后保存
itertools.islice下10行。
更新 要排除具有ip / mac地址的行:
import collectionsimport itertoolsimport re # <---import sysaddr_pattern = re.compile( r'bd{1,3}.d{1,3}.d{1,3}.d{1,3}b|' r'b[da-f]{2}:[da-f]{2}:[da-f]{2}:[da-f]{2}:[da-f]{2}:[da-f]{2}b', flags=re.IGNORECASE) # <--with open('huge-file') as f: before = collections.deque(maxlen=10) for line in f: if addr_pattern.search(line): # <--- continue # <--- if 'word' in line: sys.stdout.writelines(before) sys.stdout.write(line) sys.stdout.writelines(itertools.islice(f, 10)) break before.append(line)
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)