查找文件,返回查找特定文字中前n个匹配到的行

xiaoxiao2021-02-28  59

from collections import deque # 一个很实用的队列。 def search(lines, search_data, max_len=5): find_data_list = deque(maxlen=max_len)#设置函数里面最多保存的数量 for i in lines: if search_data in i: find_data_list.append(search_data) yield i,find_data_list #使用yield返回数据。 if len(find_data_list) == 5: return path = "/home/guo/PycharmProjects/hellotest/tst.csv" with open(path) as f: for searchline, search_data_list in search(f, "16:00", 4): for i in search_data_list: print(i, end="******") print(searchline) print("-" * 30)
转载请注明原文地址: https://www.6miu.com/read-2049974.html

最新回复(0)