【发布时间】:2014-07-08 10:52:23
【问题描述】:
我正在尝试为 WhatsApp 的对话日志编写解析器。问题末尾的最小日志文件。
在这个日志中,有两种消息,普通的,语法是
date time: Name: Message
如您所见,Message 可以换行,名称可以包含:。
第二种消息是“事件”消息,可以是以下类型:
date time: Name joined
date time: Name left
date time: Name was removed
date time: Name changed the subject to “GroupName”
date time: Name changed the group icon
我试着写了一些正则表达式,但我遇到的困难有几个:如何处理多行消息,如何解析Name字段(因为在:上拆分不起作用),如何构建一个正则表达式仅识别来自当前在组中的发件人的消息,最后如何解析特殊消息(例如,解析搜索 join as last word 这不是一个好主意)。
如何解析这样的日志文件并将所有内容移到字典中?
更准确地说,要回答评论中的问题,我正在考虑的输出类似于嵌套字典: 其中第一级的键是每个发送者,第二级区分“事件”(例如加入、离开等)和“消息”,并将所有内容作为元组列表。
>>>datab[Sender1]['Events']
>>>[('Joined',data1,time1),('Left',data2,time2]
>>>datab[Sender2]['Messages']
>>>[(data1,time1,Message1),(data2,time2,Message2)]
但如果你能想到更智能的格式,那就去吧!
29/03/14 15:48:05: John Smith changed the subject to “Test”
29/03/14 16:10:39: John Smith joined
29/03/14 16:10:40: Person:2 joined
29/03/14 16:10:40: John Smith: Hello!
29/03/14 16:11:40: Person:2: some random words,
29/03/14 16:12:40: Person3 joined
29/03/14 16:13:40: John Smith: Hello!Test message with newline
Another line of the same message
Another line.
29/03/14 16:14:43: Person:2: Test message using as last word joined
29/03/14 16:15:57: Person3 left
29/03/14 16:17:16: Person3 joined
29/03/14 16:18:21: Person:2 changed the group icon
29/03/14 16:19:16: Person3 was removed
29/03/14 16:20:43: Person:2: Test message using as last word left
【问题讨论】:
-
对于上述输入,您的预期输出是什么?
-
@Avinash 我正在编辑带有输出详细信息的问题