I have a dictionary whose keys needs to be converted to a datetime object and format it to an understandable format. This is the dictionary
blah = {
"COLUMNNAME": "Some thing",
"RID": 6,
"Y202009": 15791.68,
"Y202006": 8149.99,
"Y202003": 27252.75,
"Y201912": 19885.85,
"Y201909": 18748.21,
"rowno": 5.0
}
And this is the function to convert the keys into desired format
def dict_process(idict):
del idict["RID"]
del idict['rowno']
for key, val in idict.items():
try:
new_key = str(datetime.strptime(key,"Y%Y%m").strftime("%Y %b"))
idict[new_key] = idict.pop(key)
except:
None
return None
But this function returns a dictionary which is like this
{'COLUMNNAME': 'Net Sales',
'Y201912': 19885.85,
'Y201909': 18748.21,
'2020 Sep': 15791.68,
'2020 Jun': 8149.99,
'2020 Mar': 27252.75}
Could anyone help me with why the formatter is not working for years that contain 2019?
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…