Python Forum
help with accessing .txt file and performing actions
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
help with accessing .txt file and performing actions
#1
Please forgive me in advance I am new and this has been doing my head in for ages, I just need to bash heads with someone to hopefully help any help would be much appreciated.

I have the following files:

tasks.txt file:

Output:
sam;needs doing;now;2004-04-12;2024-03-05;No sam;please do;gym;2024-05-20;2024-03-05;No admin;do;sd;2004-12-12;2024-03-05;No dam;dam;dam;2024-12-12;2024-03-05;No
This is read as - user task assigned to; What needs doing; description of task; Due date; When task was added; is task complete.

user.txt file:

Output:
admin;password sam;westlake amy;sam dam;dam
This is read as username:password

I'm trying to create 2 .txt files:

task_overview.txt - contain total number of tasks generated, total number of tasks completed, total number of tasks uncompleted, total number of tasks not completed and overdue, percentage of tasks incomplete, percentage of tasks overdue.u

user_overview.txt - contain total number of users registered, total number of tasks generated, then for each user total number of tasks assigned to that user, percentage of total number of tasks assigned to that user, percentage tasks assigned to that user not completed, percentage tasks assigned to that user that must still be completed, percentage of tasks to that user that have not been completed and are overdue.

I have the below code that counts the total amount of tasks and then adds it to stat_dictionary, I'm guessing I need to do the same with the other stats and then print them out in a user friendly way in either task_overview.txt or user_overview.txt.

elif menu == 'gr':
                  
        stat_dictionary = {
                    "Total": 0,
                    "Incomplete": 0,
                    "Overdue": 0,
                    "Percentage Incomplete": 0,
                    "Percentage Overdue": 0
                    }
        
        task_list = open("tasks.txt", "r")
        
        for count, line in enumerate(task_list):
            pass
        stat_dictionary["Total"] += count + 1
        stat_dictionary["Incomplete"]
        
        print(stat_dictionary)
What I am struggling with is specifically taking the part I need from the txt file (e.g. date) and then passing it through stat_dictionary and in to it relevant file
Gribouillis write Mar-07-2024, 01:30 PM:
Please post all code, output and errors (it it's entirety) between their respective tags. Refer to BBCode help topic on how to post. Use the "Preview Post" button to make sure the code is presented as you expect before hitting the "Post Reply/Thread" button.
Reply
#2
You could use a csv.reader() to process the records
from collections import namedtuple
import csv
import io

Task = namedtuple('Task', 'user action desc due created complete')

file = io.StringIO("""\
sam;needs doing;now;2004-04-12;2024-03-05;No
sam;please do;gym;2024-05-20;2024-03-05;No
admin;do;sd;2004-12-12;2024-03-05;No
dam;dam;dam;2024-12-12;2024-03-05;No
""")

for row in csv.reader(file, delimiter=';'):
    task = Task(*row)
    print(task)
    print(task.created)
Output:
Task(user='sam', action='needs doing', desc='now', due='2004-04-12', created='2024-03-05', complete='No') 2024-03-05 Task(user='sam', action='please do', desc='gym', due='2024-05-20', created='2024-03-05', complete='No') 2024-03-05 Task(user='admin', action='do', desc='sd', due='2004-12-12', created='2024-03-05', complete='No') 2024-03-05 Task(user='dam', action='dam', desc='dam', due='2024-12-12', created='2024-03-05', complete='No') 2024-03-05
« We can solve any problem by introducing an extra level of indirection »
Reply
#3
I would use pandas.

To read the task.txt file.
import pandas as pd

tasks = pd.read_csv(
    "task.txt",
    sep=";",
    names=["user", "action", "desc", "due", "created", "complete"],
    parse_dates=["due", "created"])
tasks.complete = tasks.complete=="Yes"
print(tasks)
Output:
user action desc due created complete 0 sam needs doing now 2004-04-12 2024-03-05 False 1 sam please do gym 2024-05-20 2024-03-05 True 2 admin do sd 2004-12-12 2024-03-05 False 3 dam dam dam 2024-12-12 2024-03-05 False
To get the number of tasks for each user:
print(tasks.groupby("user").size())
Output:
user admin 1 dam 1 sam 2 dtype: int64
To get a list of completed or uncompleted tasks.
print("", "Completed Tasks", tasks[tasks.complete], sep="\n")
print("", "Uncompleted Tasks", tasks[~tasks.complete], sep="\n")
Output:
Completed Tasks user action desc due created complete 1 sam please do gym 2024-05-20 2024-03-05 True Uncompleted Tasks user action desc due created complete 0 sam needs doing now 2004-04-12 2024-03-05 False 2 admin do sd 2004-12-12 2024-03-05 False 3 dam dam dam 2024-12-12 2024-03-05 False
Reply
#4
To find out if the task is overdue, you need to compare the time now with the Due Date. You can read up on the datetime module, it's very handy!

Maybe this helps. Run myApp() in your IDE, change 1 thing at a time, see what happens.

from datetime import date, time, datetime

def myApp():
    text = '''text
    sam;needs doing;now;2004-04-12;2024-03-05;No
    sam;please do;gym;2024-05-20;2024-03-05;No
    admin;do;sd;2004-12-12;2024-03-05;No
    dam;dam;dam;2024-12-12;2024-03-05;No
    '''
    # read as
    # 0 user task assigned to; 1 What needs doing; 2 description of task; 3 Due date; 4 When task was added; 5 is task complete
    # 6 items

    path2text = '/home/pedro/tmp/mydata.txt'

    # put text in a file just for fun
    with open(path2text, 'w') as mf:
        mf.write(text)
        
    # open file and remove leading or trailing whitespace, including \n
    with open(path2text, 'r') as mf:
        task_list = [x.strip() for x in mf.readlines()]

    for t in task_list:
        tlist = t.split(';')
        print(f'There are {len(tlist)} items in this list.')
        # in case of less, or more, than 6 items
        if not len(tlist) == 6:
            print('Input data problem skipping this line ... ')
            continue
        print(tlist)
        
    stat_dictionary = {
                "Total": 0,
                "Incomplete": 0,
                "Overdue": 0,
                "Percentage Incomplete": 0,
                "Percentage Overdue": 0
                }

    for t in task_list:
        tlist = t.split(';')
        # in case of less, or more, than 6 items
        if not len(tlist) == 6:
            print('Input data problem skipping this line ... ')
            continue
        print(tlist)
        print(f'There are {len(t.split(";"))} items in this list.')
        stat_dictionary["Total"] +=1
        if tlist[-1] == 'No':
            stat_dictionary["Incomplete"] +=1
        dt = datetime.strptime(tlist[3], "%Y-%m-%d")
        secs = dt.timestamp()
        now = datetime.now()
        nowsecs = now.timestamp()
        if secs < nowsecs and tlist[-1] == 'No':
            stat_dictionary["Overdue"] +=1
        stat_dictionary["Percentage Incomplete"] = round(stat_dictionary["Incomplete"] / stat_dictionary["Total"] * 100, 2)
        stat_dictionary["Percentage Overdue"] = round(stat_dictionary["Incomplete"] / stat_dictionary["Total"] * 100, 2)

    for item in stat_dictionary.items():
        print(item)
Reply
#5
Quote:To find out if the task is overdue
To do this in pandas.
import pandas as pd
from datetime import datetime

tasks = pd.read_csv(
    "tasks.txt",
    sep=";",
    names=["user", "action", "desc", "created", "due", "complete"],
    parse_dates=["due", "created"])
tasks.complete = tasks.complete=="Yes"

print("All tasks", tasks, sep="\n")
print("\nAwaiting completion")
print(tasks[~tasks.complete])
today = datetime.strptime("2024-06-15", "%Y-%m-%d")
print("\nOverdue\nToday =", today.strftime("%Y-%m-%d"))
print(tasks[(~tasks.complete) & (tasks.due < today)])
Output:
All tasks user action desc created due complete 0 am needs doing now 2004-04-12 2024-03-05 False 1 sam please do gym 2024-05-20 2024-07-05 False 2 admin do sd 2004-12-12 2024-05-05 False 3 dam dam dam 2024-12-12 2024-03-05 True Awaiting completion user action desc created due complete 0 am needs doing now 2004-04-12 2024-03-05 False 1 sam please do gym 2024-05-20 2024-07-05 False 2 admin do sd 2004-12-12 2024-05-05 False Overdue Today = 2024-06-15 user action desc created due complete 0 am needs doing now 2004-04-12 2024-03-05 False 2 admin do sd 2004-12-12 2024-05-05 False
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
Question Timing actions with Python dangermaus33 0 1,012 Apr-19-2022, 10:08 PM
Last Post: dangermaus33
  Loop different actions for an array Tibovdv 4 2,769 Mar-25-2021, 06:46 PM
Last Post: jefsummers
  One-time actions when iterating ClassicalSoul 1 1,738 Apr-23-2020, 07:39 PM
Last Post: bowlofred

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020