[Tutor] group txt files by month
Asokan Pichai
pasokan at talentsprint.com
Tue Apr 3 07:14:06 CEST 2012
On Tue, Apr 3, 2012 at 9:29 AM, questions anon <questions.anon at gmail.com> wrote:
> I think what I am trying to do is relatively easy but can't get my head
> around how to do it.
> I have a list of txt files that contain daily rainfall for many years. I
> would like to produce a list that contains the year-month and the max, min
> and mean of rainfall for each month.
> My main question at this stage is how to group the files by each month for
> each year?
> They are set out like:
> r20110101.txt
> r20110102.txt
> r20110103.txt
> r20110104.txt
> r20110105.txt
> r20110106.txt
> r20110107.txt
> r20110108.txt
> r20110109.txt
> r20110110.txt
> r20110111.txt
> r20110112.txt
> r20110113.txt
> r20110114.txt
> r20110115.txt
> r20110116.txt
> r20110117.txt
> r20110118.txt
>
> and so on for each day for many years.
>
> so far I am able to open each file and calculate the max, min and mean for
> each file (see below) but not sure about grouping to monthly for each year.
# ---------------------
Monthwise = {}
# ----------------------
> MainFolder=r"E:/Rainfalldata/"
> outputFolder=r"E:/test/"
> for (path, dirs, files) in os.walk(MainFolder):
> path=path+'/'
> for fname in files:
> if fname.endswith('.txt'):
> filename=path+fname
> f=np.genfromtxt(filename, skip_header=6)
> print f.max(), f.min(), f.mean()
Replace the last two lines with
# --------------------------
Monthwise[fname[1:7]] = .np.genfromtxt(filename, skip_header=6)
# -------------------------
Now at the end you have a dictionary whose keys are the strings of type '201012'
and the values are the f.
You can now iterate over the sorted keys of Monthwise
and print appropriately
[Ideal output etc SNIPPED]
HTH
Asokan Pichai
"I have come to believe that the whole world is an enigma, a harmless
enigma that is made terrible by our own mad attempt to interpret it as
though it had an underlying truth."
-- Umberto Eco
More information about the Tutor
mailing list