How to sort date in python?

Related searches

I trying to sort the date inside my list, but the dates comes after a string element [EQUIP-X]. First using regex, take the only date and tried to sort. It doesn't work!

I thought to split the string [EQUIP-X] and Date.

files = [filename for root, dirs, files in os.walk(path) for filename in files for date in dateList if filename.endswith(date+".log")]
for item in files:
 reg = re.search(r"(.+]).(\d{2}.\d{2}.\d{4})",item)
 equip = reg.group(1)
 data = reg.group(2)
 namefile = data+'.'+equip 
 print item
  • group(1) - [EQUIP-X]
  • group(2) - Date

Sample String:

[EQUIP-4].02.05.2019.log
[EQUIP-2].01.05.2019.log
[EQUIP-1].30.04.2019.log
[EQUIP-3].29.04.2019.log
[EQUIP-1].01.05.2019.log
[EQUIP-5].30.04.2019.log
[EQUIP-1].29.04.2019.log
[EQUIP-5].30.04.2019.log
[EQUIP-3].30.04.2019.log
[EQUIP-1].29.04.2019.log
[EQUIP-2].02.05.2019.log

Following this tutorial, there is not attribute 'sort' for 'str' object, once I'm not manipulating 'date' but 'str'. What is the better way to do it? The idea was to split and handle with date and after join all

You can just sort based on the end of the string minus the last 4 characters (the file extension) parsed as a date. Since the date format is zero padded, it should always be 10 characters long hence the string splice starting from -14 (10 for date + 4 for extension)

from datetime import datetime

files = ['[EQUIP-4].02.05.2019.log',
'[EQUIP-2].01.05.2019.log',
'[EQUIP-1].30.04.2019.log',
'[EQUIP-3].29.04.2019.log',
'[EQUIP-1].01.05.2019.log',
'[EQUIP-5].30.04.2019.log',
'[EQUIP-1].29.04.2019.log',
'[EQUIP-5].30.04.2019.log',
'[EQUIP-3].30.04.2019.log',
'[EQUIP-1].29.04.2019.log',
'[EQUIP-2].02.05.2019.log']

files.sort(key=lambda x: datetime.strptime(x[-14:-4], '%d.%m.%Y'))
print(files)
['[EQUIP-3].29.04.2019.log',
'[EQUIP-1].29.04.2019.log',
'[EQUIP-1].29.04.2019.log',
'[EQUIP-1].30.04.2019.log',
'[EQUIP-5].30.04.2019.log',
'[EQUIP-5].30.04.2019.log',
'[EQUIP-3].30.04.2019.log',
'[EQUIP-2].01.05.2019.log',
'[EQUIP-1].01.05.2019.log',
'[EQUIP-4].02.05.2019.log',
'[EQUIP-2].02.05.2019.log']

sort dates in python array, To sort a Python date string list using the sort function, you'll have to convert the dates in objects and apply the sort on them. For this you can use the key named attribute of the sort function and provide it a lambda that creates a datetime object for each date and compares them based on this date object. To sort a Python date string list using the sort function, you'll have to convert the dates in objects and apply the sort on them. For this you can use the key named attribute of the sort function and provide it a lambda that creates a datetime object for each date and compares them based on this date object.

The python sort function has a key parameter that lets you modify an element before sorting it.

This example extracts the number from the end of the string and sorts by it.

a = ['hello 123', 'pumpkin 542', 'muffin 342']

def get_important_part(string):
    return int(string.split()[1])

print(sorted(a, key=get_important_part))

returns

['hello 123', 'muffin 342', 'pumpkin 542']

How to sort a Python date string list?, Use list. sort() to sort a list of datetime objects in-place. yesterday = date. today() - timedelta(days=1) today = date. today() tomorrow = date. today() + timedelta(days=1) date_list =[today, tomorrow, yesterday] print(date_list) date_list. sort() print(date_list) How to sort the below array of dates on python 2.4 timestamps = ['2011-06-2', '2011-08-05', '2011-02-04', '2010-1-14', '2010-12-13', '2010-1-12', '2010-2-11', '2010-2

Why not work with strptime and strftime?

dates = ['02.05.2019', '20.05.2019', '11.05.2019', '30.05.2019', '08.05.2019', '09.05.2019']
dates_obj = [datetime.strptime(x,'%d.%m.%Y') for x in dates]
dates_sorted = sorted(dates_obj)
dates_sorted = [x.strftime('%d.%m.%Y') for x in dates_sorted]
print (dates_sorted)

['02/05/2019', '08/05/2019', '09/05/2019', '11/05/2019', '20/05/2019', '30/05/2019']

How to sort a list of datetime objects in Python, Call pandas.DataFrame.sort_values(by=column_name) to sort pandas. DataFrame by the contents of a column named column_name . Before doing this � You can use Python to sort a list by using sorted (). In this example, a list of integers is defined, and then sorted () is called with the numbers variable as the argument: >>>. >>> numbers = [6, 9, 3, 1] >>> sorted(numbers) [1, 3, 6, 9] >>> numbers [6, 9, 3, 1] The output from this code is a new, sorted list.

You can convert your list into a panda dataframe then do the sorting accordingly. Sort by year, month and day then convert the index to a list. Then display the sorted values by index (iloc).

import pandas as pd
df = pd.DataFrame([('[EQUIP-4].02.05.2019.log')
,('[EQUIP-2].01.05.2019.log')
,('[EQUIP-1].30.04.2019.log')
,('[EQUIP-3].29.04.2019.log')
,('[EQUIP-1].01.05.2019.log')
,('[EQUIP-5].30.04.2019.log')
,('[EQUIP-1].29.04.2019.log')
,('[EQUIP-5].30.04.2019.log')
,('[EQUIP-3].30.04.2019.log')
,('[EQUIP-1].29.04.2019.log')
,('[EQUIP-2].02.05.2019.log')], columns = ['file'])

df.iloc[df['file'] \
      .map(lambda x: pd.to_datetime(x[-14:-4])) \
      .sort_values() \
      .index \
      .tolist()]

Result:

                 file
1   [EQUIP-2].01.05.2019.log
4   [EQUIP-1].01.05.2019.log
0   [EQUIP-4].02.05.2019.log
10  [EQUIP-2].02.05.2019.log
3   [EQUIP-3].29.04.2019.log
6   [EQUIP-1].29.04.2019.log
9   [EQUIP-1].29.04.2019.log
2   [EQUIP-1].30.04.2019.log
5   [EQUIP-5].30.04.2019.log
7   [EQUIP-5].30.04.2019.log
8   [EQUIP-3].30.04.2019.log

How to sort a Pandas DataFrame by date in Python, Python sorting functionality offers robust features to do basic sorting or customize ordering at a granular level. In this course, you’ll learn how to sort various types of data in different data structures, customize the order, and work with two different methods of sorting in Python.

Combining @ddg's and @Sayse's suggestion, you can try:

import re
from datetime import datetime

files = ["[EQUIP-4].02.05.2019.log", ...]

files.sort(key = lambda item: datetime.strptime(re.search(r"(?=.)(\d{2}.\d{2}.\d{4})(?=.)", item).group(0), '%d.%m.%Y'), reverse=False)

or in a more readable way:

def getSortValue(item):
  reg = re.search(r"(?=.)(\d{2}.\d{2}.\d{4})(?=.)", item)
  data = reg.group(0)
  return datetime.strptime(data, '%d.%m.%Y')

files.sort(key = getSortValue, reverse = False)

Output:

print('\n'.join(files))

[EQUIP-3].29.04.2019.log
[EQUIP-1].29.04.2019.log
[EQUIP-1].29.04.2019.log
[EQUIP-1].30.04.2019.log
[EQUIP-5].30.04.2019.log
[EQUIP-5].30.04.2019.log
[EQUIP-3].30.04.2019.log
[EQUIP-2].01.05.2019.log
[EQUIP-1].01.05.2019.log
[EQUIP-4].02.05.2019.log
[EQUIP-2].02.05.2019.log

Python sorting functionality offers robust features to do basic sorting or customize ordering at a granular level. In this course, you’ll learn how to sort various types of data in different data structures, customize the order, and work with two different methods of sorting in Python.

Python List sort() The sort() method sorts the elements of a given list in a specific ascending or descending order. The syntax of the sort() method is:

Like C++ sort(), Java sort() and other languages, python also provides built in function to sort. The sort function can be used to sort the list in both ascending and descending order. To sort the list in ascending order.

Example 2: Sort Pandas DataFrame in a descending order. Alternatively, you can sort the Brand column in a descending order. To do that, simply add the condition of ascending=False in this manner: df.sort_values(by=['Brand'], inplace=True, ascending=False) And the complete Python code would be: