用def函数写出python代码,用argparse。能在任何文件夹读取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个曲线图,在一张画布上,标注每条线的标题
时间: 2024-03-10 19:47:17 浏览: 56
以下是示例代码,实现了上述要求:
```python
import argparse
import re
import csv
import matplotlib.pyplot as plt
def read_log(filename):
with open(filename) as f:
lines = f.readlines()
start_index = None
for i, line in enumerate(lines):
if "My name is kitty" in line:
start_index = i + 1
if start_index is None:
raise ValueError("Cannot find 'My name is kitty' in the log file")
return lines[start_index:]
def remove_duplicates(lines):
return list(set(lines))
def write_csv(lines, output_filename):
with open(output_filename, 'w', newline='') as f:
writer = csv.writer(f)
for line in lines:
writer.writerow(line.split())
def extract_columns(lines):
header = lines[0].split()
num_columns = len(header)
regex = re.compile(r"ABC|DFG|TBC|DHHG|lop|kki|CAT|DOG|Fish|food")
category_indices = [[] for _ in range(4)]
for i, col in enumerate(header):
match = regex.search(col)
if match:
category_index = None
if match.group() in ["ABC", "DFG", "TBC", "DHHG", "lop", "kki"]:
category_index = 0
elif match.group().startswith("CAT"):
category_index = 1
elif match.group().startswith("DOG"):
category_index = 2
elif match.group() in ["Fish", "food"]:
category_index = 3
if category_index is not None:
category_indices[category_index].append(i)
return num_columns, category_indices
def plot_lines(data, header, category_indices):
fig, ax = plt.subplots()
for indices, label in zip(category_indices, header):
x = range(len(data))
y = [data[i][j] for i, j in enumerate(indices)]
ax.plot(x, y, label=label)
ax.legend()
plt.show()
def main():
parser = argparse.ArgumentParser(description="Process log file and output CSV file")
parser.add_argument("filename", help="log file name")
parser.add_argument("output_filename", help="output CSV file name")
args = parser.parse_args()
lines = read_log(args.filename)
lines = remove_duplicates(lines)
write_csv(lines, args.output_filename)
num_columns, category_indices = extract_columns(lines)
with open(args.output_filename) as f:
reader = csv.reader(f)
data = list(reader)
header = data[0]
data = data[1:]
if len(header) != num_columns:
raise ValueError("Header does not match number of columns in CSV file")
plot_lines(data, header, category_indices)
if __name__ == "__main__":
main()
```
使用示例:
```bash
python my_program.py log.txt output.csv
```
其中 `log.txt` 是原始日志文件,`output.csv` 是输出的 CSV 文件。程序会自动根据日志文件的内容提取数据并绘制曲线图。
阅读全文