用def函数写出python代码,有注释。用argparse,pandas。功能是能在任何文件夹读取Log并选择分类进行绘图。具体如下: 1.读取log,当读取到最后一次出现"My name is kitty"时,从下一行开始读 2.删除所有重复的行,只留第一行 3.逐行写入csv 4.第1行按照正则规则1提取单词,然后分列,分列后才知道有多少列,分列符号也不知道有几个空格。 5.从第2行开始按照正则规则2分列后,才知道有多少列,分列符号也不知道有几个空格。 6.读取CSV文件Output 7.把标题行分为4类,第一类是标题为ABC,DFG,TBC,DHHG,lop,kki的6列,第二类是CAT开头的几列,第三类是DOG开头的几列,第四类是Fish和food开头的几列 8.把4类标题画成4个曲线图,在一张画布上,标注每条线的标题 9.在一张画布从上到下分成4份,把4类标题画成4个曲线图,颜色随机,刻度比例适当 10.查看4个图的时候,可以使用筛选器,可以让我选择每次看哪个图
时间: 2024-03-10 19:51:15 浏览: 60
python实现读取文件夹下所有excel文件内容上传MySQL数据库,并附带生成log文件,同时上传成功之后清空文件夹
好的,下面是代码实现:
```python
import argparse
import pandas as pd
import re
import matplotlib.pyplot as plt
import random
# 定义 argparse
parser = argparse.ArgumentParser()
parser.add_argument("log_file", help="log 文件的路径")
parser.add_argument("csv_file", help="CSV 文件的路径")
args = parser.parse_args()
# 读取 log 文件
with open(args.log_file, 'r') as f:
log_lines = f.readlines()
# 找到 "My name is kitty" 并从下一行开始读取
start_line = 0
for i, line in enumerate(log_lines):
if "My name is kitty" in line:
start_line = i + 1
data_lines = log_lines[start_line:]
# 删除重复行并写入 csv 文件
with open(args.csv_file, 'w') as f:
f.write("line\n")
unique_lines = set()
for line in data_lines:
if line not in unique_lines:
unique_lines.add(line)
f.write(line)
# 读取 csv 文件
data = pd.read_csv(args.csv_file)
# 正则规则 1:提取单词
regex1 = re.compile(r"\b\w+\b")
matches = regex1.findall(data.iloc[0]["line"])
num_cols = len(matches)
# 分列
data = pd.DataFrame([x.split()[:num_cols] for x in data["line"]], columns=matches[:num_cols])
# 正则规则 2:分列
regex2 = re.compile(r"\s+")
num_cols = len(regex2.split(data.iloc[1]["line"]))
# 分列
data = pd.DataFrame([x.split()[:num_cols] for x in data["line"]], columns=data.columns)
# 分类标题
category1 = ["ABC", "DFG", "TBC", "DHHG", "lop", "kki"]
category2 = [col for col in data.columns if col.startswith("CAT")]
category3 = [col for col in data.columns if col.startswith("DOG")]
category4 = [col for col in data.columns if col.startswith(("Fish", "food"))]
# 画图
fig, axs = plt.subplots(4, 1, figsize=(10, 12))
# 画分类 1 的图
axs[0].set_title("Category 1")
for col in category1:
axs[0].plot(data[col], label=col)
axs[0].legend()
# 画分类 2 的图
axs[1].set_title("Category 2")
for col in category2:
axs[1].plot(data[col], label=col)
axs[1].legend()
# 画分类 3 的图
axs[2].set_title("Category 3")
for col in category3:
axs[2].plot(data[col], label=col)
axs[2].legend()
# 画分类 4 的图
axs[3].set_title("Category 4")
for col in category4:
axs[3].plot(data[col], label=col)
axs[3].legend()
# 随机设置颜色
colors = [plt.cm.tab10(i) for i in range(10)]
random.shuffle(colors)
# 画随机颜色的图
fig, axs = plt.subplots(4, 1, figsize=(10, 12))
for i, category in enumerate([category1, category2, category3, category4]):
axs[i].set_title("Category {}".format(i+1))
for j, col in enumerate(category):
axs[i].plot(data[col], label=col, color=colors[j])
axs[i].legend()
# 筛选器
plt.show()
category = input("Which category do you want to see? ")
if category == "1":
fig, ax = plt.subplots()
ax.set_title("Category 1")
for col in category1:
ax.plot(data[col], label=col)
ax.legend()
elif category == "2":
fig, ax = plt.subplots()
ax.set_title("Category 2")
for col in category2:
ax.plot(data[col], label=col)
ax.legend()
elif category == "3":
fig, ax = plt.subplots()
ax.set_title("Category 3")
for col in category3:
ax.plot(data[col], label=col)
ax.legend()
elif category == "4":
fig, ax = plt.subplots()
ax.set_title("Category 4")
for col in category4:
ax.plot(data[col], label=col)
ax.legend()
else:
print("Invalid category")
```
阅读全文