下面是一个Python程序的示例,用于读取mbox-short.txt文件并计算每天每个小时的邮件分布情况:
file_name = "mbox-short.txt"
# 创建一个字典来保存每天每个小时的邮件计数
email_distribution = {}
# 打开文件
with open(file_name, "r") as file:
# 逐行读取文件内容
for line in file:
# 如果行以"From "开头,则表示这是一封邮件的起始行
if line.startswith("From "):
# 提取日期和时间信息
date_time = line.split()[5]
date = date_time.split(":")[0]
hour = date_time.split(":")[1]
# 更新邮件计数
if date not in email_distribution:
email_distribution[date] = {}
if hour not in email_distribution[date]:
email_distribution[date][hour] = 1
else:
email_distribution[date][hour] += 1
# 打印每天每个小时的邮件分布情况
for date, hour_count in email_distribution.items():
for hour, count in hour_count.items():
print("Date: {} Hour: {}:00 - {}:00 Count: {}".format(date, hour, int(hour)+1, count))
该程序首先打开mbox-short.txt文件,然后逐行读取文件内容。当遇到以"From "开头的行时,表示这是一封邮件的起始行。程序提取出日期和时间信息,并使用字典email_distribution来保存每天每个小时的邮件计数。最后,程序打印出每天每个小时的邮件分布情况。
请注意,该程序假设mbox-short.txt文件的每封邮件都以"From "开头。如果文件的格式不同,您可能需要根据实际情况进行修改。