簡體   English   中英

從 txt 對不同用戶的特定任務進行分組。 python中的文件

[英]Grouping specific tasks for various users from a txt. file in python

我有一個任務,我必須創建一個 txt 文件中列出的每個用戶任務的摘要。 例如,摘要必須包括分配給每個用戶的任務數、已完成的任務數、逾期的任務數等。我想出了一個非常簡單的系統,為每個用戶創建一個列表,但這根本不是模塊化的,因為如果創建了新用戶,它將完全忽略它們。 我真的很難想出一些模塊化邏輯,所以任何幫助都將不勝感激。

文本文件的結構如下:用戶名、任務標題、任務描述、截止日期、發布日期、完成(是/否) 例如:

james, hello, hello world, 30/Jun/2022, 26 May 2022, Yes

我沒有很多代碼,因為我不確定從哪里開始,但這是我到目前為止所擁有的:

# For user overview:

# Creating new txt.file for summary to be output in

user_ov = open('user_overview.txt','w+')

# Opening and reading task text file

task_file = open('tasks.txt','r+')
task_content = task_file.readlines()

# Isolating specific users tasks by name

for line in task_content:
    split_task = line.split(", ")
    user_name = split_task_cont[0]
    
    for user_name in task_content:
        user_name = []
        
# Calculating the length of all the created lists  

num_users = len(file_content)

# Writing the values into the txt file

user_ov.write(f"The total number of users is {length_user}\n")
user_ov.write(f"The total number of tasks is {length_task}\n")

您可以使用字典來存儲數據,並且可以輕松地向其中添加新用戶。

示例代碼

file_content = {}
with open('user_task_data.txt', 'r') as f:
    for line in f.readlines():
        split_list = line.split(',')
        assert len(split_list) == 6
        
        name = split_list[0].strip()
        task_name = split_list[1].strip()
        task_details = {}
        task_details['task_detail'] = split_list[2].strip()
        task_details['due_date'] = split_list[3].strip()
        task_details['issued_date'] = split_list[4].strip()
        task_details['completed'] = split_list[5].strip()
        tasks = {}
        tasks[task_name] = task_details
        if name in file_content:
            if task_name in file_content[name]:
                file_content[name][task_name].update(task_details)
            else:
                file_content[name][task_name] = task_details
        else: 
            file_content[name] = tasks
            
print('total users: ', len(file_content)) 
print('total tasks: ', sum(len(file_content[user]) for user in file_content))

說輸入是

James, task1, task 1 details, 30/Jun/2022, 26 May 2022, Yes
Alan, task2, task 2 details, 03/Jul/2022, 05 Apr 2022, Yes
Bob, task3, task 3 details, 26/Sep/2022, 21 Feb 2022, No
James, task4, task 4 details, 15/Jun/2022, 17 Mar 2022, No

輸出將是

{'Alan': {'task2': {'completed': 'Yes', 'due_date': '03/Jul/2022', 'issued_date': '05 Apr 2022', 'task_detail': 'task 2 details' }}, 'Bob': {'task3': {'completed': 'No', 'due_date': '26/Sep/2022', 'issued_date': '2022 年 2 月 21 日', 'task_detail': '任務 3 details'}}, 'James': {'task1': {'completed': 'Yes', 'due_date': '30/Jun/2022', 'issued_date': '2022 年 5 月 26 日', 'task_detail': '任務 1 詳細信息'},'task4':{'已完成':'否','due_date':'15/Jun/2022','issued_date':'2022 年 3 月 17 日','task_detail':'任務 4 詳細信息' }}}

我使用 dict 而不是 list 的原因是有更好的時間復雜度。 您可以使用數據庫獲得更好的結果

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM