写出python代码,能在任何文件夹读取Log。具体如下: 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 09:51:38 浏览: 27
以下是Python代码实现:
```python
import re
import csv
import os
import matplotlib.pyplot as plt
# 正则规则1和2
rule1 = r"(\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}),(.+),(\d+),(\d+),(.+)"
rule2 = r"(.+),(.+),(.+),(.+),(.+),(.+),(.+),(.+),(.+),(.+)"
# 读取log文件
def read_log(file_path):
with open(file_path, 'r') as f:
lines = f.readlines()
index = 0
for i, line in enumerate(lines):
if "My name is kitty" in line:
index = i + 1
return lines[index:]
# 删除重复行
def remove_duplicate(lines):
new_lines = []
for line in lines:
if line not in new_lines:
new_lines.append(line)
return new_lines
# 写入CSV文件
def write_csv(lines, output_file):
with open(output_file, 'w', newline='') as f:
writer = csv.writer(f)
writer.writerow(['Date', 'Name', 'ID1', 'ID2', 'Content'])
for line in lines:
match = re.match(rule1, line)
if match:
writer.writerow(match.groups())
else:
match = re.match(rule2, line)
if match:
writer.writerow(match.groups())
# 读取CSV文件
def read_csv(file_path):
with open(file_path, 'r') as f:
reader = csv.reader(f)
rows = [row for row in reader]
return rows[1:], rows[0]
# 按照标题分类
def classify_title(headers):
abc_headers = []
cat_headers = []
dog_headers = []
fish_headers = []
for header in headers:
if header.startswith('ABC') or header.startswith('DFG'):
abc_headers.append(header)
elif header.startswith('CAT'):
cat_headers.append(header)
elif header.startswith('DOG'):
dog_headers.append(header)
elif header.startswith('Fish'):
fish_headers.append(header)
return abc_headers, cat_headers, dog_headers, fish_headers
# 画曲线图
def plot_curve(data, title):
x = range(len(data))
plt.plot(x, data)
plt.title(title)
plt.show()
if __name__ == '__main__':
# 读取log文件
log_file = 'test.log'
lines = read_log(log_file)
# 删除重复行
new_lines = remove_duplicate(lines)
# 写入CSV文件
output_file = 'output.csv'
write_csv(new_lines, output_file)
# 读取CSV文件
data, headers = read_csv(output_file)
# 按照标题分类
abc_headers, cat_headers, dog_headers, fish_headers = classify_title(headers)
# 画曲线图
for header in abc_headers:
index = headers.index(header)
column = [float(row[index]) for row in data]
plot_curve(column, header)
for header in cat_headers:
index = headers.index(header)
column = [float(row[index]) for row in data]
plot_curve(column, header)
for header in dog_headers:
index = headers.index(header)
column = [float(row[index]) for row in data]
plot_curve(column, header)
for header in fish_headers:
index = headers.index(header)
column = [float(row[index]) for row in data]
plot_curve(column, header)
```
注意:由于没有提供log文件和输出文件,无法保证代码能够完全运行成功。