Development of an internal social media platform with personalised dashboards for students
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

compat.py 631B

1234567891011121314151617181920212223
  1. """Handle reading and writing JSON in UTF-8, on Python 3 and 2."""
  2. import json
  3. import sys
  4. if sys.version_info[0] >= 3:
  5. # Python 3
  6. def write_json(obj, path, **kwargs):
  7. with open(path, 'w', encoding='utf-8') as f:
  8. json.dump(obj, f, **kwargs)
  9. def read_json(path):
  10. with open(path, 'r', encoding='utf-8') as f:
  11. return json.load(f)
  12. else:
  13. # Python 2
  14. def write_json(obj, path, **kwargs):
  15. with open(path, 'wb') as f:
  16. json.dump(obj, f, encoding='utf-8', **kwargs)
  17. def read_json(path):
  18. with open(path, 'rb') as f:
  19. return json.load(f)