sorting a list of dicts by a computed field

MRAB python at mrabarnett.plus.com
Tue Jan 31 15:34:14 EST 2017


On 2017-01-31 20:26, 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?
>
The module 'datetime' contains a class called 'datetime'. Judging by 
your exception, 'datetime' is the module.

Try "datetime.datetime.strptime" instead.




More information about the Python-list mailing list