我正在使用python从日志文件中解析数据。 当我尝试添加日期和时间时,我不再拥有有效的JSON。
一直到处看看,我没有遇到格式错误或unicode问题
with open("messages", "r") as log_file:
for line in log_file:
line_list = line.split()
log = {}
# date = datetime.strptime(" ".join(line_list[:3]), '%b %d %H:%M:%S')
# log['Date'] = date.replace(year=2019) # no year in log
log['Day'] = line_list[1]
log['Time'] = line_list[2]
# log["Month"] = datetime.strptime(
# " ".join(line_list[:3]), '%b %d %H:%M:%S').isoformat()
log["Device"] = line_list[3]
log["User_info"] = line_list[4]
log["Message"] = " ".join(line_list[6:])
logs.append(log)
print(logs)
给我:
{'Day': '8', 'Time': '16:48:38', 'Device': 'icmp-73260f', 'User_info': 'user.info', 'Message': 'SM- ADSP: SPORT resync counter: 0, state: playing'}, {'Day': '8', 'Time': '16:48:39', 'Device': 'icmp-73260f', 'User_info': 'user.info', 'Message': 'Player- received GIP input selection request: input 0'}
但是将其粘贴到JSON验证程序中会得到:
第1行解析错误: [{'Day':'17','Time': -^ 期待“ STRING”,“}”获得“未定义”