[星期维度]日志数据提取事件关键词,解析对应日期的星期计数,matplotlib绘制统计图,python

x33g5p2x  于2022-08-17 转载在 Python  
字(3.4k)|赞(0)|评价(0)|浏览(617)

**[星期维度]**日志数据提取事件关键词,解析对应日期的星期计数,matplotlib绘制统计图,python

这次把日志数据中每一行包含关键词的日期对应的星期计数,绘制统计图表

参考文:

根据星期时间统计日期总量,绘制图表,pandas,matplotlib,Python

https://zhangphil.blog.csdn.net/article/details/125934069

https://zhangphil.blog.csdn.net/article/details/125934069

日志数据提取事件关键词,解析对应时间点计数,matplotlib绘制统计图,python
https://zhangphil.blog.csdn.net/article/details/125923359

https://zhangphil.blog.csdn.net/article/details/125923359

from datetime import datetime
from pprint import pp

import pandas as pd
import matplotlib
import matplotlib.pyplot as plt

from fuzzywuzzy import fuzz
import re

FILE_PATH = r'源数据路径'
KEY = r'模糊匹配的关键词'  # 关键词1,关键词2
threshold = 80

SECTION = 'section'
SUM = 'sum'

def drawchart(df):
    myfont = matplotlib.font_manager.FontProperties(fname='C:\Windows\Fonts\msyh.ttc')
    plt.rcParams['axes.unicode_minus'] = False  # 用来正常显示负号
    plt.rc('font', family='YaHei', weight='bold')

    order = []
    name = []
    mem = []
    for d, i in zip(df.values, df.index):
        order.append(i)
        name.append(d[0])
        mem.append(int(d[1]))

    FONT_SIZE = 12

    fig, ax = plt.subplots(figsize=(15, 13))

    b = ax.barh(y=range(len(name)), width=mem, align='center', color='red')

    # 为横向水平的柱图右侧添加数据标签。
    i = 0
    for rect in b:
        w = rect.get_width()
        ax.text(x=w, y=rect.get_y() + rect.get_height() / 2, s='%d' % (int(w)),
                horizontalalignment='left', verticalalignment='center',
                fontproperties=myfont, fontsize=FONT_SIZE - 2, color='green')
        ax.text(x=w / 2, y=rect.get_y() + rect.get_height() / 2, s=str(order[i]),
                horizontalalignment='center', verticalalignment='center',
                fontproperties=myfont, fontsize=FONT_SIZE - 3, color='white')
        i = i + 1

    ax.set_yticks(range(len(name)))
    ax.set_yticklabels(name, fontsize=FONT_SIZE - 1, fontproperties=myfont)

    ax.invert_yaxis()

    ax.set_xlabel('数据', fontsize=FONT_SIZE + 2, fontproperties=myfont)
    ax.set_title('不同星期日数据点总量排名', fontsize=FONT_SIZE + 3, fontproperties=myfont)

    # 不要横坐标上的label标签。
    plt.xticks(())

    # 清除四周的边框线
    ax.get_yaxis().set_visible(True)
    for spine in ["left", "top", "right", "bottom"]:
        ax.spines[spine].set_visible(False)

    plt.subplots_adjust(left=0.15)  # 调整左侧边距

    # ax.margins(y=0.01) #缩放 zoom in

    ax.set_aspect('auto')

    plt.show()

def read_file():
    file = open(FILE_PATH, 'r', encoding='UTF-8')

    all_case_time = []

    case_count = 1
    cnt = 1
    for line in file:
        pr = fuzz.partial_ratio(line, KEY)

        if pr >= threshold:
            print('-----')
            print(f'第{case_count}件')
            case_count = case_count + 1

            try:
                # 正则匹配 xxxx年xx月xx日xx时xx分
                mat = re.search(r'\d{4}\年\d{1,2}\月\d{1,2}\日\d{1,2}\时\d{1,2}\分', line)
                t_str = mat.group().replace('\n', '')  # 去掉正则匹配到但是多余的 \n 换行符

                try:
                    object_t = datetime.strptime(t_str, "%Y年%m月%d日%H时%M分")
                    all_case_time.append(object_t.date())  # 日期提取出来,放到数组中
                    print(f'{object_t.date().strftime("%Y-%m-%d")} {object_t.weekday()}')
                except:
                    print('解析日期失败')
                    pass
            except:
                t_str = '-解析异常-'
                pass

            s = '第{number}行,相似度{ratio},时间{case_time}\n{content}'
            ss = s.format(number=cnt, ratio=pr, case_time=t_str, content=line)
            pp(ss)

        # 快速调试
        # if case_count > 100:
        #    break

        cnt = cnt + 1

    file.close()

    return all_case_time

def data_frame():
    ts = read_file()

    times = []
    for i in range(7):
        times.append({SECTION: i, SUM: 0})

    for t in ts:
        for tx in times:
            if tx[SECTION] == t.weekday():
                tx[SUM] = tx[SUM] + 1
                break

    return times

def number_to_weekday(number):
    zh = ['一', '二', '三', '四', '五', '六', '日']
    weekday = f'星期{zh[number]}'
    return weekday

if __name__ == '__main__':
    times = data_frame()

    # 数据组装成pandas数据帧。
    pd_data = []
    for t in times:
        l = [number_to_weekday(t[SECTION]), t[SUM]]
        pd_data.append(l)

    col = ['星期', '次数']
    df = pd.DataFrame(data=pd_data, columns=col)
    df = df.sort_values(by=col[1], axis=0, ascending=False)  # 降序

    # 重置索引
    df = df.reset_index(drop=True)
    df.index = df.index + 1

    # 前10名
    pp(df.head(20))
    # pp(df.values)

    drawchart(df)

变换不同关键词,得出的统计图:

相关文章