Re: How to sort the files based on the date?

2019-07-15 Thread Peter Pearson
On Mon, 15 Jul 2019 09:20:51 +0200, Peter Otten <__pete...@web.de> wrote:
> Madhavan Bomidi wrote:
>
[snip]
>> 
>> 3RIMG_01APR2018_0514_L2G_AOD.h5
>> 3RIMG_01APR2018_0544_L2G_AOD.h5
>> 3RIMG_01APR2018_0644_L2G_AOD.h5
>> 3RIMG_01APR2018_0714_L2G_AOD.h5
>> 3RIMG_01APR2018_0744_L2G_AOD.h5
[snip]
>> 
>> Can anyone suggest me how I can sort theses files in increasing order of
>> the date on the file name?
>
> Use a key function
>
> filenames = sorted(filename, key=get_datetime)
>
> get_datetime should extract date/time from the filename into a datetime 
> object. The names will then be ordered according to the datetimes' values:
>
> import glob
> import datetime
>
> def get_datetime(filename):
> parts = filename.split("_")
> return datetime.datetime.strptime(parts[1] + parts[2], "%d%b%Y%H%M")
>
> filenames = sorted(glob.glob('3RIMG_*.h5'), key=get_datetime)
>
> for fn in filenames:
> print(fn)


Gorgeous.  This is the best newsgroup ever.

-- 
To email me, substitute nowhere->runbox, invalid->com.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: How to sort the files based on the date?

2019-07-15 Thread Richard Damon
On 7/15/19 2:59 AM, Madhavan Bomidi wrote:
> I am using the following command line to sort the files:
>
> import glob
> a = sorted(glob.glob('3RIMG_*.h5')
>
> Following is the result:
>
> 3RIMG_01APR2018_0514_L2G_AOD.h5
> 3RIMG_01APR2018_0544_L2G_AOD.h5
> 3RIMG_01APR2018_0644_L2G_AOD.h5
> 3RIMG_01APR2018_0714_L2G_AOD.h5
> 3RIMG_01APR2018_0744_L2G_AOD.h5
> 3RIMG_01MAY2018_0515_L2G_AOD.h5
> 3RIMG_01MAY2018_0545_L2G_AOD.h5
> 3RIMG_01MAY2018_0615_L2G_AOD.h5
> 3RIMG_01MAY2018_0645_L2G_AOD.h5
> 3RIMG_01MAY2018_0715_L2G_AOD.h5
> 3RIMG_01MAY2018_0745_L2G_AOD.h5
> 3RIMG_01MAY2018_0815_L2G_AOD.h5.
> 3RIMG_02APR2018_0514_L2G_AOD.h5
> 3RIMG_02APR2018_0544_L2G_AOD.h5
> 3RIMG_02APR2018_0614_L2G_AOD.h5
> 3RIMG_02APR2018_0644_L2G_AOD.h5
> 3RIMG_02APR2018_0714_L2G_AOD.h5
> 3RIMG_02APR2018_0744_L2G_AOD.h5
> 3RIMG_02APR2018_0814_L2G_AOD.h5
> 3RIMG_02MAY2018_0515_L2G_AOD.h5
> 3RIMG_02MAY2018_0545_L2G_AOD.h5
> 3RIMG_02MAY2018_0615_L2G_AOD.h5
> 3RIMG_02MAY2018_0645_L2G_AOD.h5
> 3RIMG_02MAY2018_0715_L2G_AOD.h5
> 3RIMG_02MAY2018_0745_L2G_AOD.h5
> 3RIMG_02MAY2018_0815_L2G_AOD.h5
> 3RIMG_31MAR2018_0514_L2G_AOD.h5
> 3RIMG_31MAR2018_0544_L2G_AOD.h5
> 3RIMG_31MAR2018_0614_L2G_AOD.h5
> 3RIMG_31MAR2018_0644_L2G_AOD.h5
> 3RIMG_31MAR2018_0714_L2G_AOD.h5
> 3RIMG_31MAR2018_0744_L2G_AOD.h5
>
> As you can see from the list of files displayed after sorting, the files are 
> not arranged according to the increasing order of the date since the date is 
> in the DDMMM format. 
>
> Can anyone suggest me how I can sort theses files in increasing order of the 
> date on the file name?
>
> Thanks in advance

One quick comment which may or may not be applicable, as it relies on
changing the file name format would be to switch the file names to using
something like an ISO date format fo the name, ISO dates are in the
format -MM-DD (Month is a two digit number) which has the nice
attribute that the alphabetical sort is also a date sort.

If you can't do this, then as others have pointed out, you need to parse
out the date from the filename, and use that as the sort key.

-- 
Richard Damon

-- 
https://mail.python.org/mailman/listinfo/python-list


Re: How to sort the files based on the date?

2019-07-15 Thread Madhavan Bomidi
Thanks Peter. 

The following lines worked:

import glob
import datetime 

def get_datetime (filename):
parts = filename.split ("_")
return datetime.datetime.strptime (parts[1] + parts[2], "%d%b%Y%H%M") 

filenames = sorted(glob.glob('3RIMG _ *. h5'), key = get_datetime) 

-- 
https://mail.python.org/mailman/listinfo/python-list


Re: How to sort the files based on the date?

2019-07-15 Thread Peter Otten
Madhavan Bomidi wrote:

> I am using the following command line to sort the files:
> 
> import glob
> a = sorted(glob.glob('3RIMG_*.h5')
> 
> Following is the result:
> 
> 3RIMG_01APR2018_0514_L2G_AOD.h5
> 3RIMG_01APR2018_0544_L2G_AOD.h5
> 3RIMG_01APR2018_0644_L2G_AOD.h5
> 3RIMG_01APR2018_0714_L2G_AOD.h5
> 3RIMG_01APR2018_0744_L2G_AOD.h5
> 3RIMG_01MAY2018_0515_L2G_AOD.h5
> 3RIMG_01MAY2018_0545_L2G_AOD.h5
> 3RIMG_01MAY2018_0615_L2G_AOD.h5
> 3RIMG_01MAY2018_0645_L2G_AOD.h5
> 3RIMG_01MAY2018_0715_L2G_AOD.h5
> 3RIMG_01MAY2018_0745_L2G_AOD.h5
> 3RIMG_01MAY2018_0815_L2G_AOD.h5
> 3RIMG_02APR2018_0514_L2G_AOD.h5
> 3RIMG_02APR2018_0544_L2G_AOD.h5
> 3RIMG_02APR2018_0614_L2G_AOD.h5
> 3RIMG_02APR2018_0644_L2G_AOD.h5
> 3RIMG_02APR2018_0714_L2G_AOD.h5
> 3RIMG_02APR2018_0744_L2G_AOD.h5
> 3RIMG_02APR2018_0814_L2G_AOD.h5
> 3RIMG_02MAY2018_0515_L2G_AOD.h5
> 3RIMG_02MAY2018_0545_L2G_AOD.h5
> 3RIMG_02MAY2018_0615_L2G_AOD.h5
> 3RIMG_02MAY2018_0645_L2G_AOD.h5
> 3RIMG_02MAY2018_0715_L2G_AOD.h5
> 3RIMG_02MAY2018_0745_L2G_AOD.h5
> 3RIMG_02MAY2018_0815_L2G_AOD.h5
> 3RIMG_31MAR2018_0514_L2G_AOD.h5
> 3RIMG_31MAR2018_0544_L2G_AOD.h5
> 3RIMG_31MAR2018_0614_L2G_AOD.h5
> 3RIMG_31MAR2018_0644_L2G_AOD.h5
> 3RIMG_31MAR2018_0714_L2G_AOD.h5
> 3RIMG_31MAR2018_0744_L2G_AOD.h5
> 
> As you can see from the list of files displayed after sorting, the files
> are not arranged according to the increasing order of the date since the
> date is in the DDMMM format.
> 
> Can anyone suggest me how I can sort theses files in increasing order of
> the date on the file name?

Use a key function

filenames = sorted(filename, key=get_datetime)

get_datetime should extract date/time from the filename into a datetime 
object. The names will then be ordered according to the datetimes' values:

import glob
import datetime

def get_datetime(filename):
parts = filename.split("_")
return datetime.datetime.strptime(parts[1] + parts[2], "%d%b%Y%H%M")

filenames = sorted(glob.glob('3RIMG_*.h5'), key=get_datetime)

for fn in filenames:
print(fn)



-- 
https://mail.python.org/mailman/listinfo/python-list


How to sort the files based on the date?

2019-07-15 Thread Madhavan Bomidi
I am using the following command line to sort the files:

import glob
a = sorted(glob.glob('3RIMG_*.h5')

Following is the result:

3RIMG_01APR2018_0514_L2G_AOD.h5
3RIMG_01APR2018_0544_L2G_AOD.h5
3RIMG_01APR2018_0644_L2G_AOD.h5
3RIMG_01APR2018_0714_L2G_AOD.h5
3RIMG_01APR2018_0744_L2G_AOD.h5
3RIMG_01MAY2018_0515_L2G_AOD.h5
3RIMG_01MAY2018_0545_L2G_AOD.h5
3RIMG_01MAY2018_0615_L2G_AOD.h5
3RIMG_01MAY2018_0645_L2G_AOD.h5
3RIMG_01MAY2018_0715_L2G_AOD.h5
3RIMG_01MAY2018_0745_L2G_AOD.h5
3RIMG_01MAY2018_0815_L2G_AOD.h5
3RIMG_02APR2018_0514_L2G_AOD.h5
3RIMG_02APR2018_0544_L2G_AOD.h5
3RIMG_02APR2018_0614_L2G_AOD.h5
3RIMG_02APR2018_0644_L2G_AOD.h5
3RIMG_02APR2018_0714_L2G_AOD.h5
3RIMG_02APR2018_0744_L2G_AOD.h5
3RIMG_02APR2018_0814_L2G_AOD.h5
3RIMG_02MAY2018_0515_L2G_AOD.h5
3RIMG_02MAY2018_0545_L2G_AOD.h5
3RIMG_02MAY2018_0615_L2G_AOD.h5
3RIMG_02MAY2018_0645_L2G_AOD.h5
3RIMG_02MAY2018_0715_L2G_AOD.h5
3RIMG_02MAY2018_0745_L2G_AOD.h5
3RIMG_02MAY2018_0815_L2G_AOD.h5
3RIMG_31MAR2018_0514_L2G_AOD.h5
3RIMG_31MAR2018_0544_L2G_AOD.h5
3RIMG_31MAR2018_0614_L2G_AOD.h5
3RIMG_31MAR2018_0644_L2G_AOD.h5
3RIMG_31MAR2018_0714_L2G_AOD.h5
3RIMG_31MAR2018_0744_L2G_AOD.h5

As you can see from the list of files displayed after sorting, the files are 
not arranged according to the increasing order of the date since the date is in 
the DDMMM format. 

Can anyone suggest me how I can sort theses files in increasing order of the 
date on the file name?

Thanks in advance
-- 
https://mail.python.org/mailman/listinfo/python-list