extracting values by using dictionary key

Jump to solution
09-23-2014 09:51 AM
New Contributor

Hi Guys,

I have text file having two columns: 1) column 1 is in the format yyyy-mm-dd and 2) column 2 is precipitation.

Objective: to extract original value of precipitation from column 2 only for month April, May, june, July, and August (4,5,6,7,8).

Procedure: Used line.split to extract only mm from yyyy-mm-dd format from column-1.

  • made dictionary to get month and precipitation value.
  • using for k,v in dct.items() and then using a if statement to extract the corresponding month precipitation value from dictionary

Problem: I can successfully print k,v (month and precipitation) from dictionary items. However, when I am using a if statement to extract the specific month's precipitation value, I am getting blank array. I was wondering can I use .append to get precipitation in (1.8,2.1,3.3) format.



file1 = open("test.txt","r")  Growing=[] Intermediate=[] Dormant=[]  for line in file1:     line2 = line.split()     WQ = line2[1]     month = line2[0].split("-")[1]     dct1={month:WQ}     for k,v in dct1.items():         if (k ==4 or k==5 or k==6 or k==7 or k==8):             Growing.append(dct1 )     print Growing
0 Kudos
11 Replies
New Contributor

Thanks a lot! I really appreciate it! Have a good one.


0 Kudos
Esri Esteemed Contributor

You´re welcome

0 Kudos