I have a series of filenames which I need to process and they have year and Julian day in their filename, e.g. A1998237.tif.

How can I group these files based on their names, by month (e.g. Jan, Feb ...)?

This is a pre-procedure and after this I will read this files into memory. Then I will take the average of matrices associated with each of this files to produce monthly matrices.

有帮助吗?

解决方案

Parse out the julian day, file each filename into a dictionary for that month:

from datetime import datetime
import os.path

# months from 0 to 11
files_by_month = [[] for _ in range(12)]

for filename in filenames:
    dt = datetime.strptime(os.path.splitext(filename)[0][1:], '%Y%j')
    files_by_month[dt.month - 1].append(filename)

files_by_month uses 0-based indexing to store a list per month; January is 0, etc.

This assumes that the filename always starts with one letter, followed by the year + julian year day, followed by the extension. If these assumptions are incorrect you need to provide more information on what sort of patterns there are in your filenames.

Demo for your sample filename:

>>> from datetime import datetime
>>> import os.path
>>> filename = 'A1998237.tif'
>>> datetime.strptime(os.path.splitext(filename)[0][1:], '%Y%j')
datetime.datetime(1998, 8, 25, 0, 0)

which would be filed in the 'august' bucket, files_by_month[7].

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top