1.读取log,当读取到最后一次出现"My name is kitty"时,从下一行开始读 2.删除所有重复的行,只留第一行 3.第一行按照正则规则1分列后,写入新的CSV文件Output 4.从下一行开始按照正则规则2分列后,继续写入新的CSV文件Output 5.读取CSV文件Output 6.把标题行分为4类,第一类是标题为ABC,DFG的2列,第二类是CAT开头的几列,第三类是DOG开头的几列,第四类是Fish开头的几列 7.把4类标题画成曲线图
时间: 2024-03-05 10:51:25 浏览: 58
以下是可以实现这些要求的Python代码:
```python
import re
import pandas as pd
import matplotlib.pyplot as plt
# 正则规则1和规则2
regex1 = r"(\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}), (\w+), (\w+), (\d+), (\w+)"
regex2 = r"(\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}), (.*)"
# 读取log文件
with open("log.txt", "r") as f:
lines = f.readlines()
# 找到最后一次"My name is kitty"出现的位置
last_kitty_index = -1
for i, line in enumerate(lines):
if "My name is kitty" in line:
last_kitty_index = i
# 从下一行开始读取
new_lines = lines[last_kitty_index+1:]
# 删除重复的行,只留第一行
unique_lines = list(set(new_lines))
output_lines = []
# 第一行按照正则规则1分列后,写入新的CSV文件Output
for line in unique_lines:
if re.match(regex1, line):
match = re.findall(regex1, line)[0]
output_line = ",".join(match) + "\n"
output_lines.append(output_line)
# 从下一行开始按照正则规则2分列后,继续写入新的CSV文件Output
for line in unique_lines:
if re.match(regex2, line):
match = re.findall(regex2, line)[0]
output_line = ",".join(match) + "\n"
output_lines.append(output_line)
# 将结果写入Output文件
with open("Output.csv", "w") as f:
f.writelines(output_lines)
# 读取CSV文件Output
df = pd.read_csv("Output.csv")
# 把标题行分为4类
col_ABC_DFG = ["ABC", "DFG"]
col_CAT = [col for col in df.columns if col.startswith("CAT")]
col_DOG = [col for col in df.columns if col.startswith("DOG")]
col_Fish = [col for col in df.columns if col.startswith("Fish")]
# 把4类标题画成曲线图
fig, axs = plt.subplots(2, 2, figsize=(12, 8))
axs[0, 0].plot(df[col_ABC_DFG[0]], df[col_ABC_DFG[1]])
axs[0, 0].set_title("ABC and DFG")
axs[0, 0].set_xlabel("Time")
axs[0, 0].set_ylabel("Value")
for i, col in enumerate(col_CAT):
axs[0, 1].plot(df[col])
axs[0, 1].set_title("CAT")
axs[0, 1].set_xlabel("Time")
axs[0, 1].set_ylabel("Value")
for i, col in enumerate(col_DOG):
axs[1, 0].plot(df[col])
axs[1, 0].set_title("DOG")
axs[1, 0].set_xlabel("Time")
axs[1, 0].set_ylabel("Value")
for i, col in enumerate(col_Fish):
axs[1, 1].plot(df[col])
axs[1, 1].set_title("Fish")
axs[1, 1].set_xlabel("Time")
axs[1, 1].set_ylabel("Value")
plt.show()
```
阅读全文