On 01/31/2017 01:26 PM, 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?
You're not showing us all your code, so I can only make an assumption that the "datetime" you are referencing is a module import. Did you try your sort function before you stuck it in a lambda? Here's a attempt: >>> import datetime >>> datetime.strptime Traceback (most recent call last): File "<stdin>", line 1, in <module> AttributeError: 'module' object has no attribute 'strptime' Looks like the same problem you encountered. So it has nothing to do with sorting. Quite literally, your datetime symbol does not contain the attribute strptime like you thought it did. I think you meant to use datetime.datetime.strptime: >>> import datetime >>> datetime.datetime.strptime("01/02/2003","%m/%d/%Y") datetime.datetime(2003, 1, 2, 0, 0) Use the interactive interpreter to test out little things like this. It will save you a lot of hassle later on. -- https://mail.python.org/mailman/listinfo/python-list