用python提取log中的指定内容到表格中
时间: 2024-05-03 20:22:42 浏览: 175
python提取log文件内容并画出图表
假设log文件的格式如下:
```
2022-01-01 08:00:01 INFO: Starting process A
2022-01-01 08:00:02 ERROR: Process A encountered an error
2022-01-01 08:00:03 INFO: Starting process B
2022-01-01 08:00:04 INFO: Process B completed successfully
2022-01-01 08:00:05 INFO: Starting process C
2022-01-01 08:00:06 ERROR: Process C encountered an error
```
我们想要提取出所有的process及其状态(如上述log中的A、B、C以及它们的状态),并将它们存储到一个表格中。可以按照以下步骤进行:
1. 读取log文件,并将每一行的内容拆分成日期、时间、状态和process四个部分。这里使用split()函数进行拆分。
```python
log_file = open('log.txt', 'r')
rows = []
for line in log_file:
date, time, status, process = line.strip().split(' ')
rows.append([date, time, status, process])
log_file.close()
```
2. 对所有的process进行去重,并根据process生成表格的列名。
```python
# 去重
processes = list(set([row[3] for row in rows]))
# 生成表格的列名
table = [['Date', 'Time'] + processes]
# 将每个process的状态填入表格中
for row in rows:
date, time, status, process = row
if not any([date == t[0] and time == t[1] for t in table]):
table.append([date, time] + ['-' for _ in range(len(processes))])
index = processes.index(process)
table[-1][index+2] = status
```
3. 将表格输出到文件中。
```python
with open('output.csv', 'w') as f:
for row in table:
f.write(','.join(row) + '\n')
```
完整代码:
```python
log_file = open('log.txt', 'r')
rows = []
for line in log_file:
date, time, status, process = line.strip().split(' ')
rows.append([date, time, status, process])
log_file.close()
# 去重
processes = list(set([row[3] for row in rows]))
# 生成表格的列名
table = [['Date', 'Time'] + processes]
# 将每个process的状态填入表格中
for row in rows:
date, time, status, process = row
if not any([date == t[0] and time == t[1] for t in table]):
table.append([date, time] + ['-' for _ in range(len(processes))])
index = processes.index(process)
table[-1][index+2] = status
# 输出表格
with open('output.csv', 'w') as f:
for row in table:
f.write(','.join(row) + '\n')
```
阅读全文
相关推荐
; 提取查询结果中的表格内容
FileDelete, result.txt
Loop, %TotalPage%
{
MatchItemNum := 0
Loop, Read, Html\%A_Index%.html, result.txt
{
; 抽取表格字段
If (A_LoopReadLine ~= "i)[^<]+]+class=""calc""")
{
MatchItemNum++
Field := RegExReplace(A_LoopReadLine,"i)^[^>]+>()?([^<]+)<.+","$2$3")
StringReplace, Field, Field, , , All
; 表格中的字段每5个组成一条记录
ModNum := Mod(MatchItemNum,5)
If (ModNum = 1)
FileAppend, % "n" Field "t"
else
{
If (ModNum <> 0)
FileAppend, % Field "t"
else
FileAppend, % "http://gis2.sinica.edu.tw/place/" SubStr(Field,1,-4)
}
}
}
}
MsgBox, 整理完毕