python - How can I retrieving a timestamp from a dictionary and converting it to datetime object? -
i have dictionary of timestamps in form 2011-03-01 17:52:49.728883 , ids. how can retrieve latest timestamp , represent in python datetime object?
the point able use latest date of timestamp instead of currnt date in above code.
latest = datetime.now()
the structure of dictionary not entirely clear question, i'll assume keys strings containing timestamps 1 in question.
if d dictionary:
datetime.strptime(max(d.keys()),'%y-%m-%d %h:%m:%s.%f')
the above code uses fact lexicographical ordering of datetime strings sorts timestamps in chronological order. if you'd rather not rely on that, use:
max(map(lambda dt:datetime.strptime(dt,'%y-%m-%d %h:%m:%s.%f'),d.keys()))
Comments
Post a Comment