我有几个聊天记录日志,想计算每月发送和接收的消息数量。一些消息对应于文本文件中的一行,但不是全部。因此,我想在日期和时间拆分消息。然后我想从每个日期中提取月份和年份,并计算消息的数量并在字典中调整这个数字。最后,我想打印月/年和消息数。
这是源文件的样子(日期是d/m/Y):
09/10/2017, 10:55 - Name omitted: Lorem ipsum dolor sit amet, consectetur adipiscing elit.
09/10/2017, 11:17 - Name omitted: Pellentesque massa tellus, porttitor et iaculis vitae, sodales ac mauris.
Aliquam ullamcorper dictum laoreet. Proin ornare ultrices eros, ut fermentum ex accumsan at. Curabitur dignissim massa a nisi molestie, id hendrerit elit convallis.
Etiam tincidunt gravida arcu, vel lacinia tellus dignissim eu. Praesent ullamcorper neque eu tellus interdum, in semper nibh sagittis. Fusce dignissim sollicitudin mauris in tempus. Sed in magna ante.
09/10/2017, 11:29 - Name omitted: Nam eu risus laoreet, commodo neque eget, tincidunt risus. Suspendisse eu ullamcorper metus.
这是我的代码,不幸的是它不起作用。结果我得到一长串 1:
import os
import re
nummessages = {}
datafiles = ("file1.txt", "file2.txt")
for file in datafiles:
with open(file, "r", encoding="utf8") as infile:
for line in infile:
regexdate = re.compile("([0-9]{2})(\/)([0-9]{2})(\/)([0-9]{4})(,)(\s)([0-9]{2})(:)([0-9]{2})")
messages = regexdate.split(line)
for message in messages:
key = re.search("([0-9]{2})(\/)([0-9]{4})", message)
value = message.count(message)
if key in nummessages.keys():
nummessages[key].append(value)
else:
nummessages[key] = [value]
for key in sorted(nummessages.items()):
print(str(key[0]) + "\t" + str(key[1]))
我想要的输出如下所示:
09/2017: 45 messages
10/2017: 10 messages
...
我究竟做错了什么?(仅供参考,我是 Python 新手)
繁华开满天机
扬帆大鱼
相关分类