I have this code:
import glob, os
outdir = './output/'
nstring = 'testdat_2014-12-31'
nfilelist = sorted(glob.glob((outdir+'/*{}*.nc').format(nstring)))
from which I get nfilelist
:
['testdat_2014-12-31-21_H1.nc',
'testdat_2014-12-31-21_H10.nc',
'testdat_2014-12-31-21_H11.nc',
'testdat_2014-12-31-21_H12.nc',
'testdat_2014-12-31-21_H2.nc',
'testdat_2014-12-31-21_H3.nc',
'testdat_2014-12-31-21_H4.nc',
'testdat_2014-12-31-21_H5.nc',
'testdat_2014-12-31-21_H6.nc',
'testdat_2014-12-31-21_H7.nc',
'testdat_2014-12-31-21_H8.nc',
'testdat_2014-12-31-21_H9.nc']
The H1-H12 numbers at the end reflect how I want to sort it. But right now, H10-H12 is sandwiched in the middle. How can I sort from H1-H12?
Regex isn't my strong suit and I'm unable to move forward.
I tried splitting and got this far:
nfilelist[0].split('_')[-1].split('.')
['H1', 'nc']