utils.py 578 B

12345678910111213141516171819
  1. import json
  2. def read_json(filename):
  3. try:
  4. with open(filename, "r") as f:
  5. data = json.load(f)
  6. except FileNotFoundError as e:
  7. print("Warning:", e)
  8. print("Could not find json file {}, returning an empty dict".format(filename))
  9. data = dict()
  10. except ValueError as e:
  11. print("Warning:", e)
  12. print("Could not decode json data of file {}, returning an empty dict".format(filename))
  13. data = dict()
  14. return data
  15. def write_json(data, filename):
  16. with open(filename, "w") as f:
  17. json.dump(data, f)