Elegant way to store dictionary permanently with Python?

Why not dump it to a JSON file, and then load it from there where you need it?

import json

with open('my_dict.json', 'w') as f:
    json.dump(my_dict, f)

# elsewhere...

with open('my_dict.json') as f:
    my_dict = json.load(f)

Loading from JSON is fairly efficient.

Another option would be to use pickle, but unlike JSON, the files it generates aren’t human-readable so you lose out on the visual verification you liked from your old method.

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)