[Tutor] group txt files by month

questions anon questions.anon at gmail.com
Thu Apr 5 05:37:16 CEST 2012


I have been able to write up what I want to do (using glob) but I am not
sure how to loop it or simplify it to make the script more efficient.
I am currently:
-grouping the same months in a year using glob
-opening the files in a group and combining the data using a list
-finding max, min etc for the list and printing it

I need to do this for many years and therefore many months so really need a
way to make this more efficient.
Any feedback will be greatly appreciated

MainFolder=r"E:/rainfall-2011/"
OutputFolder=r"E:/test_out/"
r201101=glob.glob(MainFolder+"r201101??.txt")
r201102=glob.glob(MainFolder+"r201102??.txt")
r201103=glob.glob(MainFolder+"r201103??.txt")

rain201101=[]
rain201102=[]
rain201103=[]
monthlyrainfall=[]

for ifile in r201101:
    f=np.genfromtxt(ifile, skip_header=6)
    rain201101.append(f)

for ifile in r201102:
    f=np.genfromtxt(ifile, skip_header=6)
    rain201102.append(f)

for ifile in r201103:
    f=np.genfromtxt(ifile, skip_header=6)
    rain201103.append(f)

print "jan", np.max(rain201101), np.min(rain201101), np.mean(rain201101),
np.median(rain201101), np.std(rain201101)
print "feb", np.max(rain201102), np.min(rain201102), np.mean(rain201102),
np.median(rain201102), np.std(rain201102)
print "mar", np.max(rain201103), np.min(rain201103), np.mean(rain201103),
np.median(rain201103), np.std(rain201103)


On Thu, Apr 5, 2012 at 11:11 AM, questions anon <questions.anon at gmail.com>wrote:

> thanks for responding.
> Glob and os.walk will work but I would need to type up a separate command
> for each month of each year and that doesn't seem very efficient. Is there
> a way to make it go through and group txt files with similar filenames
> e.g something like:
> if fname.endswith('.txt')and fname[0:7]==fname[0:7]
> e.g. r20110101.txt and r20110102.txt should go together but r20110601
> should not.
> thanks
>
>
> On Tue, Apr 3, 2012 at 4:59 PM, Alan Gauld <alan.gauld at btinternet.com>wrote:
>
>> On 03/04/12 04:59, questions anon wrote:
>>
>>  I have a list of txt files that contain daily rainfall for many years.
>>> They are set out like:
>>> r20110101.txt
>>> r20110102.txt
>>> r20110103.txt
>>> and so on for each day for many years.
>>>
>>> MainFolder=r"E:/Rainfalldata/"
>>> outputFolder=r"E:/test/"
>>> for (path, dirs, files) in os.walk(MainFolder):
>>>
>>
>> If the files are all in a single folder you might be better using
>> glob.glob() rather than os.walk. You can pass a filename pattern
>> like *.txt to glob(). This might make it easier to group the
>> files by year... 2010*.txt for example.
>>
>> You can do it with walk too its just a bit more effort. But if the files
>> are in multiple folders walk() is probably  better.
>>
>> --
>> Alan G
>> Author of the Learn to Program web site
>> http://www.alan-g.me.uk/
>>
>> ______________________________**_________________
>> Tutor maillist  -  Tutor at python.org
>> To unsubscribe or change subscription options:
>> http://mail.python.org/**mailman/listinfo/tutor<http://mail.python.org/mailman/listinfo/tutor>
>>
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20120405/42caf431/attachment.html>


More information about the Tutor mailing list