Advertisement
dsuveges

JSON example

Jul 6th, 2016
161
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.87 KB | None | 0 0
  1. # Game variables are stored in a dictionary:
  2. game_data = {"pNev" : "Inid",
  3.     "pKaszt" : 1,
  4.     "pEro" : 12,
  5.     "pElet" : 550,
  6.     "pUgyesseg" : 5,
  7.     "pErtelem" : 1,
  8.     "pStamina" : 25,
  9.     "pCelzas" : 1,
  10.     "pPenz" : 2,
  11.     "pSzint" : 4,
  12.     "pExp" : 199,
  13.     "kepesPont" : 1
  14. }
  15.  
  16. # Get a list of variables:
  17. print ", ".join(game_data.keys())
  18.  
  19. # Access the value of a variable:
  20. print game_data["pExp"]
  21.  
  22. # modifying a variable:
  23. game_data["pExp"] += 1
  24.  
  25. # Access a variable again:
  26. print game_data["pExp"]
  27.  
  28. # Saving data into a file in JSON format:
  29. import json
  30. with open('mentes.txt', 'w') as outfile:
  31.     json.dump(game_data, outfile)
  32.    
  33. # Reading data back from json:
  34. with open('mentes.txt') as json_data:
  35.     game_data2 = json.load(json_data)
  36.     json_data.close()
  37.  
  38. # Now the data is read back in a new dictionary: game_data2
  39. print game_data2["pExp"]
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement