sorting a list of dicts by a computed field

Kev Dwyer kevin.p.dwyer at gmail.com
Tue Jan 31 15:33:08 EST 2017


Larry Martell wrote:

> I have a list of dicts and one item of the dict is a date in m/d/Y
> format. I want to sort by that. I tried this:
> 
> sorted(data['trends'], key=lambda k:
> datetime.strptime(k['date_time'],'%m/%d/%Y'))
> 
> But that fails with:
> 
> Exception Type: AttributeError at
> /report/CDSEM/WaferAlignment/ajax/waChart.json Exception Value: 'module'
> object has no attribute 'strptime'
> 
> How can I do this sort?

datetime.datetime.strptime?

>>> import datetime
>>> datetime.strptime
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: module 'datetime' has no attribute 'strptime'
>>> datetime.datetime.strptime
<built-in method strptime of type object at 0x7f716c2f5d20>




More information about the Python-list mailing list