亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

在日期(使用正則表達式)拆分聊天日志文件并計算每月的消息數

在日期(使用正則表達式)拆分聊天日志文件并計算每月的消息數

慕哥9229398 2021-10-19 15:27:54
我有幾個聊天記錄日志,想計算每月發送和接收的消息數量。一些消息對應于文本文件中的一行,但不是全部。因此,我想在日期和時間拆分消息。然后我想從每個日期中提取月份和年份,并計算消息的數量并在字典中調整這個數字。最后,我想打印月/年和消息數。這是源文件的樣子(日期是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 osimport renummessages = {}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 messages10/2017: 10 messages...我究竟做錯了什么?(僅供參考,我是 Python 新手)
查看完整描述

2 回答

?
繁華開滿天機

TA貢獻1816條經驗 獲得超4個贊

嘗試這個:


此解決方案的主要思想是解析日志的月份和年份,并將其用作data字典中的鍵?,F在,對于匹配相同月份和年份的每個日志,字典的值都會增加 1


data = {} # outside


for file in datafiles:

  with open(file, "r", encoding="utf8") as infile:

    for l in infile: 

      m = re.match(r'\d{2}/(\d{2})/(\d{4})', l)

      if m:

        key = '{}/{}'.format(m.group(1), m.group(2))

        if key not in data.keys():

          data[key] = 0

        data[key] += 1


# printing

for k in data:

  print '{}: {} messages'.format(k, data[k])

lines 參考日志文件中的每一行

http://img1.sycdn.imooc.com//616e73940001e5c501840079.jpg

查看完整回答
反對 回復 2021-10-19
?
揚帆大魚

TA貢獻1799條經驗 獲得超9個贊

使用 collections.defaultdict


前任:


import re

from collections import defaultdict


result = defaultdict(int)

with open(file, "r", encoding="utf8") as infile:

    for line in infile:                              #Iterate Each line

        line = line.strip()

        m = re.match("(\d{2}/(\d{2})/(\d{4}))", line)   #Check if line starts with date

        if m:

            result["{}/{}".format(m.group(2), m.group(3))] += 1   #form month/year and get count. 

print(result)


查看完整回答
反對 回復 2021-10-19
  • 2 回答
  • 0 關注
  • 188 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號