Created
January 24, 2013 15:34
-
-
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)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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