-1

I'm using this to add business days to a certain date. it works just like an excel function:

WORKDAY(start_date,business days to add,[holidays])

and I would like it to read a list of holidays from a .txt file like this:

2017-09-18
2017-09-19
2017-10-09
2017-12-25

how can I do this? it would be easier to load it from an excel or a txt. is just fine?

Thanks in advance!

Zeinab Abbasimazar
  • 9,835
  • 23
  • 82
  • 131
Richie
  • 19
  • 2
  • 4
    What have you tried so far? Please post your code. – James Nov 22 '17 at 13:02
  • from datetime import datetime import workdays Holidays = open("C:\holidays.txt").readlines() print (Holidays) friday = datetime.strptime('SEP 15 2017', '%b %d %Y') wanted_day = workdays.workday(friday,1,Holidays) print(friday) print(wanted_day) – Richie Nov 22 '17 at 14:30
  • i get this: holidays = [x for x in holidays if x.weekday() not in weekends ] AttributeError: 'str' object has no attribute 'weekday' – Richie Nov 22 '17 at 14:34

1 Answers1

0

This will read from file.txt the dates. Once read it will add 5 workingdays to each date:

from datetime import datetime
from datetime import date

business_days_to_add = 5
holidays = [date(year=2009,month=12,day=25)] 

for line in open("file.txt"):
    print(workday(datetime.strptime(line.strip(), "%Y-%m-%d")), business_days_to_add, holidays)

HTH

Charlie
  • 1,750
  • 15
  • 20
  • thank you for your help!! but how could I turn into dates a list of string dates? i guess than when i read the txt, a string list is created. but i need it to be a date list so i can use it in the function – Richie Nov 22 '17 at 14:37
  • datetime.strptime(, ) converts a string into da datetime. In my code I read the file line by line, than for each line I convert the string (containing the date) into a datetime object. – Charlie Nov 22 '17 at 14:52
  • see offical python documentation for more details: https://docs.python.org/3.6/library/datetime.html#strftime-strptime-behavior – Charlie Nov 22 '17 at 14:53
  • if you like my answer plase mark it as the correct one. Thanks! – Charlie Nov 22 '17 at 14:58
  • @ Charlie when I tried to read the txt. python reads it with and extra character at the begining. isntead of" 2017-10-01" it reads "2017-10-01".do you have any idea of what is happening? – Richie Nov 22 '17 at 15:22
  • it is a file encoding problem. Those chars means that your file is UTF-8 encoded and it is using BOM chars at the beginning (see https://en.wikipedia.org/wiki/Byte_order_mark). You can strip off the BOM with a good text editor setting the encoding to utf-8 (if you are on windows you can do it with notepad++, using the encoding menu). – Charlie Nov 22 '17 at 15:54
  • You can solve it also via python, see https://stackoverflow.com/questions/8898294/convert-utf-8-with-bom-to-utf-8-with-no-bom-in-python – Charlie Nov 22 '17 at 15:55