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.
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) print Growing
Solved! Go to Solution.
Using the attached sample file and the code below I obtained a list of precipitation values:
infile = r"D:\Xander\txt\precip.txt"
sep = "\t" # example TAB as seperator
lst_months = [4, 5, 6, 7, 8]
growing = []
header = 1
i = 0
with open(infile,"r") as f:
for line in f:
i += 1
if i > header:
line = line.strip("\n")
data = line.split(sep)
if len(data) == 2:
print data
date = data[0]
prec = float(data[1])
month = int(date.split("-")[1])
if month in lst_months:
growing.append(prec)
print growing
Kind regards, Xander
Using the attached sample file and the code below I obtained a list of precipitation values:
infile = r"D:\Xander\txt\precip.txt"
sep = "\t" # example TAB as seperator
lst_months = [4, 5, 6, 7, 8]
growing = []
header = 1
i = 0
with open(infile,"r") as f:
for line in f:
i += 1
if i > header:
line = line.strip("\n")
data = line.split(sep)
if len(data) == 2:
print data
date = data[0]
prec = float(data[1])
month = int(date.split("-")[1])
if month in lst_months:
growing.append(prec)
print growing
Kind regards, Xander
Hi Xander,
Thanks a lot for your help! I was stuck and it really helped me.Yeah, the code is working perfect. Appreciate it. Have a good one!
Sean
Hi Sean, you´re welcome. Can you mark the question as answered?
Kind regards, Xander
How do I mark as answered. I looked at and found a link called as mark as helpful and did that. Is that what you wanted me to do?
There should be a star "Correct Answer" that you can press next to the post that answered you question.
I think I marked as answer now. Thank you!
Yes you did. Thanks!
Hi Xander,
Just a silly question. I want to extract the corresponding date of the precip along with the precip value. For that, I added one line code: growing.append(float(date)) after growing.append(prec) in your code.
However, I got this error:"ValueError: invalid literal for float(): 2007-05-07". Do you mind to look at it?
Thank you very much for your time and help!
Sean
Hi Sean,
Basically the error is telling you that the date (2007-05-07) is not a float (decimal value). The other thing is that your code will result in a list that holds a precipitation value, a date, a precipitation value, a date, etc. It would probably be more appropriate to work with a nested list like this:
date = data[0]
prec = float(data[1])
lst_date_prec = [date, prec]
month = int(date.split("-")[1])
if month in lst_months:
growing.append(lst_date_prec)
This results i a list where each item contains a list of a precipitation value and the corresponding date. It really depends on what you want to do with the list...
Kind regards, Xander