Skip to content

Instantly share code, notes, and snippets.

@akhenakh
Created January 24, 2013 15:34
Show Gist options
  • Save akhenakh/4623208 to your computer and use it in GitHub Desktop.
Save akhenakh/4623208 to your computer and use it in GitHub Desktop.
convert date json entries to datetime objet while loading with json.loads (usefull to import data from Django datadump)
import json
import datetime
import re
DATE_FORMAT = '%Y-%m-%d %H:%M:%S'
def datetime_parser(entry):
for k, v in entry.items():
if isinstance(v, basestring) and re.search("^[0-9]{4}-[0-9]{2}-[0-9]{2} [0-9]{2}:[0-9]{2}:[0-9]{2}$", v):
try:
entry[k] = datetime.datetime.strptime(v, DATE_FORMAT)
except:
pass
return entry
data = json.loads(open("kdl.json").read(), object_hook=datetime_parser)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment