I need an efficient way to write files containing dictionaries including datetime, and then be able to read them as dicts. Dicts like these:
my_dict = {'1.0': [datetime.datetime(2000, 1, 1, 0, 0, 0, 000000, tzinfo=tzutc())], '2.0': [datetime.datetime(2000, 1, 1, 0, 0, 0, 000000, tzinfo=tzutc())]}
Trying to dump with json:
with open("my_file.json", 'w+') as f:
json.dump(my_dict, f)
TypeError: Object of type 'datetime' is not JSON serializable
Also tried writing the entire dict as a String and then importing it with yaml, which almost worked, but got the indexing messed up.
with open("my_file", 'w+') as f:
f.write(str(my_dict))
with open("my_file", 'r') as f:
s = f.read()
new_dict = yaml.load(s)
print(new_dict['1.0'][0])
Output: datetime.datetime(2000
Expected: 2000-01-01 00:00:00+00:00