Nov-26-2018, 02:12 PM
Good Day,
I'd like to load certain data concerning HOLIDAYS into a python dictionary. Later downstream in my business process I then want to take an input date 'look it up' in that dictionary to make use of other dictionary elements which tell me to add or subtract a day from that input date and print it out/use it elsewhere for finding information about that 'adjusted' date. For this post, all I want to do, is read in my .csv and create a dictionary 'row' for each row in the .csv.
The dictionary has this format roughly in pseudo-code: {Datetocompareinputto:Date in string format like '20181225',
Day of week of that date: string like 'Monday', 'Tuesday' etc.
Description: like 'Christmas 2018'
Days to add: Integer 1,2,3
Days to subtract: Negative Integer -1,-2,-3
Here is my code:
{'Holiday': 0.0005263157894736842, 'Dayofwk': 'Sunday', 'Desc': 'dummy', 'Daystoadd': 1, 'Daystosubtract': -1, '20160101': ('20160101', 'Friday', "New Year's Day", '1', '-1'), '20160118': ('20160118', 'Monday', 'Martin Luther King Jr. Day', '1', '-1')more....}
I have looked at various posts online and videos, but am chasing my tail around. With a LIST I can use .append method, but with dictionary all I see is vague rumblings that [ITEM] might mean something in referencing the 'THING' I want to hook on to to simply ADD ENTRIES to my dictionary. I get some results but feel like, nah man this is too hard and just not right yet. In sum I just want my Date String to be the lookup value that is unique, BUT it must also have in that same dictionary entry, the other 4 elements tied to it. I would prefer to have each element, be an independent 'item' within each dictionary entry keyed by that date. What is best practice to simply add my .csv rows to my dictionary in that manner, row by row??
Thank you for your help.
I'd like to load certain data concerning HOLIDAYS into a python dictionary. Later downstream in my business process I then want to take an input date 'look it up' in that dictionary to make use of other dictionary elements which tell me to add or subtract a day from that input date and print it out/use it elsewhere for finding information about that 'adjusted' date. For this post, all I want to do, is read in my .csv and create a dictionary 'row' for each row in the .csv.
The dictionary has this format roughly in pseudo-code: {Datetocompareinputto:Date in string format like '20181225',
Day of week of that date: string like 'Monday', 'Tuesday' etc.
Description: like 'Christmas 2018'
Days to add: Integer 1,2,3
Days to subtract: Negative Integer -1,-2,-3
Here is my code:
#Open file, load dictionary, then close file reader=csv.reader(Holiday_File) Holiday_DICT={'Holiday':1/1/1900,'Dayofwk':"Sunday",'Desc':"dummy",'Daystoadd':1,'Daystosubtract':-1} for row in reader: #print(row[0],row[1],row[2],row[3],row[4]) item=row[0] Holiday_DICT[item]=row[0],row[1],row[2],row[3],row[4] Holiday_File.close()I get results like this when I print(Holiday_DICT):
{'Holiday': 0.0005263157894736842, 'Dayofwk': 'Sunday', 'Desc': 'dummy', 'Daystoadd': 1, 'Daystosubtract': -1, '20160101': ('20160101', 'Friday', "New Year's Day", '1', '-1'), '20160118': ('20160118', 'Monday', 'Martin Luther King Jr. Day', '1', '-1')more....}
I have looked at various posts online and videos, but am chasing my tail around. With a LIST I can use .append method, but with dictionary all I see is vague rumblings that [ITEM] might mean something in referencing the 'THING' I want to hook on to to simply ADD ENTRIES to my dictionary. I get some results but feel like, nah man this is too hard and just not right yet. In sum I just want my Date String to be the lookup value that is unique, BUT it must also have in that same dictionary entry, the other 4 elements tied to it. I would prefer to have each element, be an independent 'item' within each dictionary entry keyed by that date. What is best practice to simply add my .csv rows to my dictionary in that manner, row by row??
Thank you for your help.