写出python代码 1.读取log,当读取到最后一次出现"My name is kitty"时,从下一行开始读 2.删除所有重复的行,只留第一行 3.第一行按照正则规则1分列后,写入新的CSV文件Output 4.从下一行开始按照正则规则2分列后,继续写入新的CSV文件Output
时间: 2024-03-05 10:51:24 浏览: 64
解决python cv2.imread 读取中文路径的图片返回为None的问题
5星 · 资源好评率100%
以下是可以实现上述要求的Python代码:
```python
import re
# 正则规则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)
```
阅读全文