python通过关键字获取txt文件整行,并将结果进行排序,方便做参数对比

import os
import math
import re

def get_mml_for_keyword(sourepath, keywordpath, IsSort=True):
    """
    获取含有某个或某些关键字的行 如:“RNCID=XXX”
        sourepath:待处理的源数据
        keywordpath:过滤条件或方法(关键字)
        IsSort:是否排序,True:是,False:否
    :return: result
    """    
    # 需要处理的源数据
    soure = open(sourepath, 'r', encoding='utf-8')
    # 过滤条件或方法:根据关键字过滤
    keyword = open(keywordpath, 'r', encoding='utf-8')
    lines = keyword.readlines()
    iter_soure = iter(soure)
    temp = []
    for l in lines:
        for ll in iter_soure:
            if l.strip() in ll:
                temp.append(ll)
    if IsSort:
        temp.sort()
    soure.close()
    keyword.close()
    # 输出结果
    result = open(r"D:\pycharm\01 Get_MML\result.txt", 'w+', encoding='utf-8')
    result.writelines(temp)
    result.close()
    return result

if __name__ == '__main__':
    # 原数据路径
    path_scr = r"D:\pycharm\01 Get_MML\119.txt"   
    path_filter = r"D:\pycharm\01 Get_MML\keyword.txt"
    get_mml_for_keyword(path_scr, path_filter)
    

上一篇:Archi - 一个热榜功能怎么设计,怎么设计缓存?


下一篇:Vue列表过滤