2

Hi I'm trying to make a schedule for one year where I want to write a textfile containing every single day with information of activities. The code is WRONG but I need help with changing it so that I does want I want it to do.

import pandas as pd
a = ['football', 'read']  # between 01-12-2020 - 30-04-2021
b = ['read', 'sleep']  # between 01-05-2021 - 31-08-2021
c = ['read', 'eat']  # between 01-09-2021 - 30-11-2021


with open("food.txt", "w") as f:
    for i in a:
        f.write(f"\n")
        s = pd.date_range(start='2020-12-1', end='2021-11-30')
        if '2020-12-1'<=s<='2021-04-30':
            f.write(f'{i},{s}')

#I'm trying to make a textfile that match up the activities with all dates.
# For example I want the text file to write:
# 2020-12-1
# football
# read 

# 2020-12-2
# football
# read 

# ...

# 2021-04-30
# read 
# sleep
#etc...
#If there are better ways of making this I would appreciate it, but the activities must be from a 
# inside list

1 Answers1

1

I am not sure if this is what you want.

But you could us satrt and end dates for the if statements.

And iteratre threw the overall date list.

I think with a littel bit modification this could work for you.

import pandas as pd
a = ['football', 'read']  # between 01-12-2020 - 30-04-2021
start_a = pd.to_datetime('01-12-2020')
end_a = pd.to_datetime('30-04-2021')
b = ['read', 'sleep']  # between 01-05-2021 - 31-08-2021
start_b = pd.to_datetime('01-05-2021')
end_b = pd.to_datetime('31-08-2021')
c = ['read', 'eat']  # between 01-09-2021 - 30-11-2021
start_c = pd.to_datetime('01-09-2021')
end_c = pd.to_datetime('30-11-2021')


with open("food.txt", "w") as f:
    f.write(f"\n")
    s = pd.date_range(start='2020-12-1', end='2021-11-30')

    for date in s:
        f.write(str(date) + "\n")
        if end_a >= date >= start_a:
            for element in a:
                f.write(element + "\n")

        if end_b >= date >= start_b:
            for element in b:
                f.write(element + "\n")

        if end_c >= date >= start_c:
            for element in c:
                f.write(element + "\n")

        f.write("\n\n")

Regarding your question about duplicates: You could first write all elememts to a tmp list and check for duplicates. Then only write once to the file.

So maybe change the for loop to something like that:

        for date in s:
            tmp = []
            f.write(str(date) + "\n")
            if end_a >= date >= start_a:
                for element in a:
                    if element not in tmp:
                        tmp.append(element)

            if end_b >= date >= start_b:
                for element in b:
                    if element not in tmp:
                        tmp.append(element)

            if end_c >= date >= start_c:
                for element in c:
                    if element not in tmp:
                        tmp.append(element)

            for element in tmp:
                f.write(element + "\n")

            f.write("\n\n")

So this could be a final version.

2021-04-30 Is the last day football apears.

2021-08-31 Is the last day sleep apears and 2021-01-05 the first.

import pandas as pd
a = ['football', 'read']  # between 01-12-2020 - 30-04-2021
start_a = pd.to_datetime('01-12-2020').to_pydatetime()
end_a = pd.to_datetime('30-04-2021').to_pydatetime()
b = ['read', 'sleep']  # between 01-05-2021 - 31-08-2021
start_b = pd.to_datetime('01-05-2021').to_pydatetime()
end_b = pd.to_datetime('31-08-2021').to_pydatetime()
c = ['read', 'eat']  # between 01-09-2021 - 30-11-2021
start_c = pd.to_datetime('01-09-2021').to_pydatetime()
end_c = pd.to_datetime('30-11-2021').to_pydatetime()


with open("food.txt", "w") as f:
    f.write(f"\n")
    s = pd.date_range(start='2020-12-1', end='2021-11-30').to_pydatetime()

    for date in s:
        tmp = []
        f.write(str(date) + "\n")
        if date >= start_a and date <= end_a:
            for element in a:
                if element not in tmp:
                    tmp.append(element)

        if date >= start_b and date <= end_b:
            for element in b:
                if element not in tmp:
                    tmp.append(element)

        if date >= start_c and date <= end_c:
            for element in c:
                if element not in tmp:
                    tmp.append(element)

        for element in tmp:
            f.write(element + "\n")

        f.write("\n\n")

different output files:

import pandas as pd
import datetime
import calendar
# (month,day,year)
a = ['football', 'read']  # between 01-12-2020 - 30-04-2021
start_a = pd.to_datetime('12-01-2020').to_pydatetime()
end_a = pd.to_datetime('04-30-2021').to_pydatetime()
b = ['read', 'sleep']  # between 01-05-2021 - 31-08-2021
start_b = pd.to_datetime('05-01-2021').to_pydatetime()
end_b = pd.to_datetime('08-31-2021').to_pydatetime()
c = ['read', 'eat']  # between 01-09-2021 - 30-11-2021
start_c = pd.to_datetime('09-01-2021').to_pydatetime()
end_c = pd.to_datetime('11-30-2021').to_pydatetime()

s = pd.date_range(start='2020-12-1', end='2021-11-30').to_pydatetime()


def findDay(date):
    born = datetime.datetime.strptime(date, '%Y %m %d').weekday()
    return (calendar.day_name[born])


for date in s:
    file_name = str(date).replace(" 00:00:00", "") + ".txt"
    with open(file_name, "w") as f:
        f.write(str(date).replace(" 00:00:00", "") + " " +
                findDay(str(date).replace(" 00:00:00", "").replace("-", " ")) + "\n")
        tmp = []
        if date >= start_a and date <= end_a:
            for element in a:
                if element not in tmp:
                    tmp.append(element)

        if date >= start_b and date <= end_b:
            for element in b:
                if element not in tmp:
                    tmp.append(element)

        if date >= start_c and date <= end_c:
            for element in c:
                if element not in tmp:
                    tmp.append(element)

        for element in tmp:
            f.write(element + "\n")

Simon
  • 129
  • 7
  • But in the textfile from 2021-01-05 I see: football read read sleep –  Nov 21 '20 at 16:14
  • Do you know how I can change that so read doesnt appear twice? @Simon –  Nov 21 '20 at 16:15
  • I made an edit regarding the duplicates. @Carl-ErikPettersson – Simon Nov 21 '20 at 16:34
  • Again thank you! The edit gave much better output, however in 2021-01-05 and forward I get 3 outputs. Are there any small detail missing @Simon? I get: football read sleep –  Nov 21 '20 at 16:40
  • First I found a mistake in my answer I removed it from the code (first one). And something isnt right with comparing end date. I try to figure it out. @Carl-ErikPettersson – Simon Nov 21 '20 at 17:16
  • I appreciate it so much!!!!! Hope you find the misstake @Simon –  Nov 21 '20 at 17:24
  • I hope this now works as intended. Pls check if everything is right now. Btw I think there was a misunderstanding. It is year-month-day. So I think with 2021-01-05 you meant 2021-05-01. @Carl-ErikPettersson – Simon Nov 21 '20 at 17:39
  • Hi again @Simon! I have one question. Is it possible to make one textfile for one day they put it all in a empty map? Totally 365 text-files? –  Nov 23 '20 at 15:06
  • hi, sry for the late answer. I made an edit about different output files. I also added weekdays. You may have to play wiht file_name or dates. @Carl-ErikPettersson – Simon Nov 25 '20 at 18:02