python过滤文件

#!/usr/bin/env python
#coding=utf-8
  
def read_regular_list(path):
    regular_list = list()
    with open(path, 'r') as file_handle:
        for row in file_handle:
            regular_list.append(row.strip())
    return regular_list
  
def filte_del_file(from_file, to_file, del_list):
    with open(from_file, 'r') as file_handle_from:
        with open(to_file, 'w') as file_handle_to:
            for row in file_handle_from:
                if not any(key_word in row for key_word in del_list):
                    file_handle_to.write(row)




def filte_file_save(from_file, to_file, save_list):
with open(from_file,'r') as file_handle_from:
  with open(to_file,'w') as file_handle_to:
  for row in file_handle_from:
  if any(key_word in row for key_word in save_list):
  file_handle_to.write(row)


if __name__ == '__main__':
    del_list = read_regular_list(r"del.txt")        #读取过滤规则
    filte_del_file(r"user.log", "output.txt", del_list)#过滤文件
    save_list = read_regular_list(r"save.txt") #读取过滤规则
    filte_file_save(r'user.log',"saveOutput.txt",save_list)#过滤文件
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值