Serializing datetime objects to JSON in Python

June 08, 2012

Reading time ~1 minute

I ran into a problem when writing Python datetime objects to a JSON object. It can be solved like this:

date_handler = lambda obj: obj.isoformat() if isinstance(obj, datetime.datetime) else None
for doc in docs:
    temp_file.write(json.dumps(doc, default = date_handler))

The important part here is the lamba which is producing ISO formatted strings from a datetime object. Then the json.dumps takes the argument default pointing at the lambda.

Running vim-airline with Maximum Awesome

I use [Maximum Awesome]( when I code in [vim](, it's a great starting place...… Continue reading

Quicksort implementation in Python

Published on August 04, 2014

Fibonacci generator in Python

Published on August 04, 2014