我正在尝试编写日志解析脚本。 我收到了一个日志混乱的文件。每个特定日志的第一行都会有时间戳,所以我想用它来排序。无法创建字典
例如,
10:48开始 。 。 10:50开始 。 。 10:42开始 第一行将包含关键词'开始'和时间戳。 '开始'和下一'开始'之间的界限是一组。我想根据他们的时间戳将所有这些集合排序在日志文件中。
代码逻辑: 我想创建字典,在这里我将选择这个时间,并将其分配为'钥匙'和该日志集的文本值。然后我将字典中的“键”排序,并按文件中的排序顺序打印它们的“值”。 但是我得到错误“类型错误:unhashable类型:‘名单’”
write1 = False
x = 0
search3 = "start"
matched = dict()
matched = {}
# fo is a list which is defined elsewhre in the code.
for line in fo:
if search3 in line:
#got the Hello2 printed which indicates script enters this loop
print('hello2')
write1 = True
x +=1
time = line.split()[3]
name1 = [time.split(':')[0] +":"+time.split(':')[1] + ":"+ time.split(':')[2]]
matched[name1] = line
elif write1:
matched[name1] += line
print(matched.keys())
请让我知道,如果我的逻辑,我做的方式是正确的?
字典键不应该是一个列表。 “name1”在这里列出。把它作为字符串或元组。 – Vineesh