Advertisement
Guest User

Untitled

a guest
Jun 26th, 2017
53
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.06 KB | None | 0 0
  1. # Basic example of dumping to json
  2. import json # Module used to dump with json
  3. __author__ = "Kishuna"
  4.  
  5. # Filename
  6.  
  7. def save(file, data):
  8. with open(file, "w") as _save:
  9. json.dump(data, _save, indent=4, sort_keys=True, ensure_ascii=False)
  10. print("Saved to '{}'".format(file))
  11. # indent and sort_keys are used for pretty printing
  12. # ensure_ascii on False is used for not encoding in ASCII
  13.  
  14. def get(file):
  15. try: # Error handling
  16. with open(file, "r") as _load:
  17. return json.load(_load) # Return the data
  18. except FileNotFoundError: # If the file is not found
  19. print("File doesn't exist") # Print a reminder
  20.  
  21. def main(): # Main
  22. filename = "your_filename.json" # Your filename
  23. your_data = {"spam": "eggs"} # Your data
  24. empty_data = {} # Used for load
  25. # Used for testing purposes
  26. act = input("Save or Load: ")
  27. if act == "save":
  28. save(filename, your_data)
  29. elif act == "load":
  30. empty_data = get(filename)
  31. print(empty_data)
  32.  
  33. if __name__ == "__main__":
  34. main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement