2 回答
TA贡献1757条经验 获得超8个赞
您必须手动解析日志以收集相关数据:
import re, json
pattern = re.compile(r'.+? .+? (.+)')
logs = []
with open('data.txt') as fp:
for line in fp:
match = pattern.match(line)
if match:
try:
data = json.loads(match.group(1))
logs.append(data)
except json.JSONDecodeError:
pass
df = pd.DataFrame(logs)
要实时执行此操作,您必须监视文件的更改。
TA贡献1804条经验 获得超7个赞
这是另一种使用方法json_normalize:
import json
import re
pattern = re.compile('{.*}')
rows = []
with open('a.txt', 'r+') as f:
for line in f:
for match in re.finditer(pattern, line):
data = json.loads(match.group())
dfx = pd.json_normalize(data)
rows.append(dfx)
df = pd.concat(rows)
print(df)
message level logType timeStamp
0 Killing processes... Information User 2020-10-19T10:07:49.1386035+02:00
0 Opening applications... Information User 2020-10-19T10:07:49.4092373+02:00
添加回答
举报