Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Dictionary Basics
#1
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:

#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.
Reply


Messages In This Thread
Dictionary Basics - by pythonjm - Nov-26-2018, 02:12 PM
RE: Dictionary Basics - by ichabod801 - Nov-26-2018, 05:37 PM
RE: Dictionary Basics - by pythonjm - Nov-26-2018, 08:26 PM
RE: Dictionary Basics - by ichabod801 - Nov-26-2018, 08:34 PM
RE: Dictionary Basics - by Gribouillis - Nov-26-2018, 09:56 PM
RE: Dictionary Basics - by pythonjm - Nov-26-2018, 11:11 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Learned Python Basics, what now? muzikman 20 6,078 Oct-11-2021, 01:34 AM
Last Post: Underscore
  PYserial basics? bako 10 5,127 Apr-26-2020, 09:15 PM
Last Post: bowlofred
  [split] Debugging scripts basics tultalk 5 3,091 Apr-25-2020, 01:02 AM
Last Post: menator01
  Debugging scripts basics bako 8 3,513 Apr-24-2020, 06:18 AM
Last Post: Larz60+
  Problem with basics Wraith2102 2 2,006 Jun-17-2019, 06:51 PM
Last Post: perfringo
  What to do after learning python basics xyzabc12310000 1 2,861 May-20-2018, 11:43 PM
Last Post: micseydel

Forum Jump:

User Panel Messages

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