from glob import glob import datetime nf = open("hbv-iww_watch_nosoc_co2const_dis_almourol_daily_1971_2001_formatted.txt", "w") nf.write("YEAR" + "\t" + "DAY" + "\t" + "SIM" + "\n") #yearly steps startYear = datetime.datetime(1971, 01, 01) #start of timeseries endYear = datetime.datetime(2002, 01, 01) #end of timeseries stepYear = datetime.timedelta(days = 1) #timesteps year = [] #empty list to store the years days = [] #empty list to store the days. i = 1 #create counter to count days in year #Problem: Count 365 or 366 days per year, not 1,2,3,4,5...30/31 for each month and start from 1 again. #To solve that problem, if statement was implemented year.append(startYear.strftime("%Y")) #append years to list days.append(i) #append the very first day of the year to list (1) startYear += stepYear #alter the startdate for one day while startYear < endYear: #start looping i += 1 #alter i for each day in a year if year[-1] != startYear.strftime("%Y"): #if the last element of the list (year i.e. 1971) is unequal to the last altered year (i.e. 1978) it sets the counter back to one i=1 #set counter back to one days.append(i) #append the days (counter) of each year to list until the end of a year (or the if statement) is reached (fullfilled). year.append(startYear.strftime("%Y")) #append years to list startYear += stepYear #alter the startdate for one day print len(year) print len(days) u = -1 #counter for time for i in glob("*.txt"): if i == "hbv-iww_watch_nosoc_co2const_dis_almourol_daily_1971_2001_formatted.txt": continue; else: with open(i, "r") as originalFile: for lines in originalFile: listLines = lines.split("\t") date = int(listLines[0]) value = float(listLines[3]) if date >= 1971 and date <= 2001: u += 1 z = year[u] y = days[u] nf.write(str(z) + "\t" + str(y) + "\t" + str(value) + "\n") nf.close()