AnsweredAssumed Answered

extracting values by using dictionary key

Question asked by sean.woznicki on Sep 23, 2014
Latest reply on Sep 29, 2014 by xander_bakker

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.

 

Code:

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 [k])     print Growing

Outcomes