Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Basic example of dumping to json
- import json # Module used to dump with json
- __author__ = "Kishuna"
- # Filename
- def save(file, data):
- with open(file, "w") as _save:
- json.dump(data, _save, indent=4, sort_keys=True, ensure_ascii=False)
- print("Saved to '{}'".format(file))
- # indent and sort_keys are used for pretty printing
- # ensure_ascii on False is used for not encoding in ASCII
- def get(file):
- try: # Error handling
- with open(file, "r") as _load:
- return json.load(_load) # Return the data
- except FileNotFoundError: # If the file is not found
- print("File doesn't exist") # Print a reminder
- def main(): # Main
- filename = "your_filename.json" # Your filename
- your_data = {"spam": "eggs"} # Your data
- empty_data = {} # Used for load
- # Used for testing purposes
- act = input("Save or Load: ")
- if act == "save":
- save(filename, your_data)
- elif act == "load":
- empty_data = get(filename)
- print(empty_data)
- if __name__ == "__main__":
- main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement