Python iterating list of dates

时间:2017-04-10 01:01:54

标签: python list function loops iterable

I have 10 years data with one file for each day. Each file has a name like this:

PRISM_ppt_stable_4kmD2_20151226_bil.bil (December 26, 2015)

I want to go through each file in and put the ones with the same date but different years on a list together so there should 365 lists with 10 items each. I'm having trouble figuring out how to get this to work if anyone could help me out.

Thanks

1 个答案:

答案 0 :(得分:3)

Use reg exps to catch the date and then process it using [] operator of strings. For your sample filename the date can be caught by simple [0-9]{8} regular expression (it means "numeric character exactly 8 times).
Sample code for you:

import re
match = re.search(r'[0-9]{8}', fileName)
date = match.group()
year = date[:4]