Created
April 20, 2022 18:15
-
-
Save FerusAndBeyond/f93cf27c38f3a6708c09f5c20c17ebe7 to your computer and use it in GitHub Desktop.
Dictionary to json
This file contains 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 | |
a = dict(a=5, b=6) | |
# to json | |
json_string = json.dumps(a) | |
# json_string = '{"a": 5, "b": 6}' | |
# json to dict | |
assert a == json.loads(json_string) | |
# to json file | |
with open("dict.json", "w+") as f: | |
json.dump(a, f) | |
# from json file | |
with open("dict.json", "r") as f: | |
assert a == json.load(f) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment