分类处理获取到的数据

# !/usr/bin/env python
# -*- coding:utf-8 -*-

"""
# File       : XXXXsjfl.py
# Time       :2023/9/1 20:15
# Author     :lrtao2010
# version    :python 3.10.1
# Description:分类处理获取到的数据
"""



file_name = './shuju/shareholder.txt'
file_count = './shuju/XXXX-tj.txt'
file_add = './shuju/shareholder-add.txt'
file_new = './shuju/shareholder-new.txt'
file_unchanged = './shuju/shareholder-un.txt'
file_list = [f"{file_add}",f"{file_new}",f"{file_unchanged}"]



def classify(file_name):
    print("文件处理程序正在运行...")
    #对信息进行分类处理
    with open(file_name, encoding='utf-8')as f:
        for i in f.readlines():
            if i.strip().endswith("增加"):
                with open(file_add, 'a+', encoding='utf-8')as s_f:
                    s_f.write(f"{i.strip()}" + "\n")
            elif i.strip().endswith("新进"):
                with open(file_new, 'a+', encoding='utf-8')as s_f:
                    s_f.write(f"{i.strip()}" + "\n")
            else:
                with open(file_unchanged, 'a+', encoding='utf-8')as s_f:
                    s_f.write(f"{i.strip()}" + "\n")

    #统计次数并记录
    with open(file_count,'r',encoding='utf-8') as c_f:
        for file_str in c_f.readlines():
            file_str = file_str.strip()
            for i_file in file_list:
                with open(i_file,'r', encoding='utf-8')as i_f_r:
                    counts = 0
                    for line in i_f_r.readlines():
                        time = line.count(file_str)
                        counts += time
                    if counts != 0:
                        with open(i_file, 'a+', encoding='utf-8')as i_f_a:
                            i_f_a.write(f"{file_str}" + " " + f"{counts}" + "\n" )


    print("文件处理完成")

if __name__ == '__main__':
    classify(file_name)

 

posted @ 2024-02-28 18:30  蒙古草原狼  阅读(3)  评论(0编辑  收藏  举报