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:
- First, iterate through the list of dictionaries:
for d in List_with_Dict:
- 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()
- 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(':')])
- Reassign the value to the
"Date_Time"
key of each dictionary, with the converted time string:
d["Date_Time"] = f"{date} {time}"
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…