Python统计用户任务数:读取txt文件更新字典值的技术问题
任务统计功能实现方案
一、核心任务总数统计优化
你原有的思路存在效率问题(循环每个用户都打开一次文件),下面是更高效的实现方式:
# 读取user.txt并初始化统计字典 total_users = [] with open("user.txt", "r", encoding="utf-8") as f: for line in f: # 跳过注释行和空行,避免解析错误 if line.strip().startswith("#") or not line.strip(): continue line = line.strip().split(", ") total_users.append(line[0]) # 初始化用户任务计数字典,默认值为0 user_tasks = {username: 0 for username in total_users} # 仅打开一次tasks.txt,遍历所有任务更新计数 with open("tasks.txt", "r", encoding="utf-8") as f: for line in f: if line.strip().startswith("#") or not line.strip(): continue line_items = line.strip().split(", ") username = line_items[0] # 只统计系统内存在的用户任务 if username in user_tasks: user_tasks[username] += 1
这段代码的优势:
- 跳过文件中的注释和空行,避免无效解析
- 仅读取一次tasks.txt,大幅提升运行效率
- 过滤掉不在user.txt中的用户(比如示例中的
testing),保证统计准确性
二、扩展实现全量统计指标
如果要实现你需要的所有统计项,建议为每个用户存储更详细的任务数据,代码如下:
from datetime import datetime # 读取用户列表并初始化详细统计字典 total_users = [] with open("user.txt", "r", encoding="utf-8") as f: for line in f: if line.strip().startswith("#") or not line.strip(): continue line = line.strip().split(", ") total_users.append(line[0]) # 初始化包含所有统计字段的字典 user_stats = { username: { "total_tasks": 0, "completed_tasks": 0, "incomplete_tasks": 0, "overdue_tasks": 0 } for username in total_users } # 统计全局任务总数,用于计算占比 total_all_tasks = 0 # 遍历tasks.txt收集数据 with open("tasks.txt", "r", encoding="utf-8") as f: for line in f: if line.strip().startswith("#") or not line.strip(): continue line_items = line.strip().split(", ") username, _, _, due_date, _, completed = line_items total_all_tasks += 1 if username not in user_stats: continue # 更新总任务数 user_stats[username]["total_tasks"] += 1 # 更新完成/未完成状态 if completed == "Yes": user_stats[username]["completed_tasks"] += 1 else: user_stats[username]["incomplete_tasks"] += 1 # 日期转换与逾期判断(可替换为当前系统日期) due_date_obj = datetime.strptime(due_date, "%d %b %Y") current_date = datetime(2023, 1, 1) if due_date_obj < current_date: user_stats[username]["overdue_tasks"] += 1 # 按要求格式输出统计结果 for username, stats in user_stats.items(): total = stats["total_tasks"] completed = stats["completed_tasks"] incomplete = stats["incomplete_tasks"] overdue = stats["overdue_tasks"] print(f"Username: {username}") print(f"Total tasks assigned to user: {total}") # 处理除以0的边界情况,避免报错 pct_total = (total / total_all_tasks * 100) if total_all_tasks > 0 else 0 print(f"Percentage of total number of tasks assigned to user: {pct_total:.1f}%") pct_completed = (completed / total * 100) if total > 0 else 0 print(f"Percentage of assigned tasks completed: {pct_completed:.1f}%") pct_incomplete = (incomplete / total * 100) if total > 0 else 0 print(f"Percentage of assigned tasks to be completed: {pct_incomplete:.1f}%") pct_overdue = (overdue / incomplete * 100) if incomplete > 0 else 0 print(f"Percentage of tasks assigned to be completed that are overdue: {pct_overdue:.1f}%") print("--------------------------------------------------------------")
关键说明
- 使用
datetime模块处理日期格式转换,确保逾期判断精准 - 加入了除以0的边界判断,避免程序运行报错
- 仅统计系统内用户的任务,过滤无效数据
内容的提问来源于stack exchange,提问作者k-sheikh
相关产品推荐
相关产品推荐

