Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
211 views
in Technique[技术] by (71.8m points)

Sort dates and format date in Python

I'm working in a code that use date and time. I use .sort() for sort the elements but I have some problems with the date and time format.

List_with_Dict=[
{'Date_Time': '06/12/20 14:1:43', 'Values': ' 46.2'},
{'Date_Time': '06/12/20 13:51:43', 'Values': ' 45.3'},
{'Date_Time': '06/12/20 1:21:47', 'Values': ' 23.0'},
{'Date_Time': '06/12/20 14:17:41', 'Values': ' 46.5'},
{'Date_Time': '06/12/20 13:59:19', 'Values': ' 46.1'},
{'Date_Time': '06/12/20 13:41:43', 'Values': ' 43.9'}]
List_with_Dict.sort(reverse=False, key=lambda e: e['Date_Time']) 

for elements in List_with_Dict:
    print(elements)

The output is:

{'Date_Time': '06/12/20 13:41:43', 'Values': ' 43.9'}
{'Date_Time': '06/12/20 13:51:43', 'Values': ' 45.3'}
{'Date_Time': '06/12/20 13:59:19', 'Values': ' 46.1'}
{'Date_Time': '06/12/20 14:17:41', 'Values': ' 46.5'}
{'Date_Time': '06/12/20 14:1:43', 'Values': ' 46.2'}
{'Date_Time': '06/12/20 1:21:47', 'Values': ' 23.0'}

As you can see, the two last dictionaries have a different format and can not be sorted. There any solution for this, like a different format date? Or do I need to work in the dictionaries in change the time (h:m:s to hh:mm:ss)?

question from:https://stackoverflow.com/questions/66048388/sort-dates-and-format-date-in-python

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You can use this for loop to correct the time formats:

List_with_Dict=[
{'Date_Time': '06/12/20 14:1:43', 'Values': ' 46.2'},
{'Date_Time': '06/12/20 13:51:43', 'Values': ' 45.3'},
{'Date_Time': '06/12/20 1:21:47', 'Values': ' 23.0'},
{'Date_Time': '06/12/20 14:17:41', 'Values': ' 46.5'},
{'Date_Time': '06/12/20 13:59:19', 'Values': ' 46.1'},
{'Date_Time': '06/12/20 13:41:43', 'Values': ' 43.9'}]

for d in List_with_Dict:
    date, time = d["Date_Time"].split()
    time = ':'.join([i.ljust(2, '0') for i in time.split(':')])
    d["Date_Time"] = f"{date} {time}"

print(List_with_Dict)

Output:

[{'Date_Time': '06/12/20 14:10:43', 'Values': ' 46.2'},
 {'Date_Time': '06/12/20 13:51:43', 'Values': ' 45.3'},
 {'Date_Time': '06/12/20 10:21:47', 'Values': ' 23.0'},
 {'Date_Time': '06/12/20 14:17:41', 'Values': ' 46.5'},
 {'Date_Time': '06/12/20 13:59:19', 'Values': ' 46.1'},
 {'Date_Time': '06/12/20 13:41:43', 'Values': ' 43.9'}]

Explanation:

  1. First, iterate through the list of dictionaries:
for d in List_with_Dict:
  1. Get the value of the "Date_Time" key of each dictionary of the iterations, split the values by the space, and assign the resulting two strings to two variables as the date strings and time strings.
date, time = d["Date_Time"].split()
  1. Split the time string by the colon, pad each time element with 2 "0"s, and join the elements with the colon again.
time = ':'.join([i.ljust(2, '0') for i in time.split(':')])
  1. Reassign the value to the "Date_Time" key of each dictionary, with the converted time string:
d["Date_Time"] = f"{date} {time}"

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...