(Jan-26-2017, 12:37 AM)birdieman Wrote: 1. Why do you create/change a directory called "save" (at lease I think that is what the code does)? It changed the directory that I was using to 'Save'.
Because dictionaries is very useful data structure,some even bully Python of the use of dict anywhere.
(Jan-26-2017, 12:37 AM)birdieman Wrote: 2. After the code reads the file, is there a way to refer to my variables directly, like StartYear, rather than the typical way to refer to an element in a dictionary like data['StartYear']?
There are
data['StartYear']
or
data.get.('StartYear')
,
you can make it a variable
StartYear = data['StartYear']
but then you move from your dictionary to Python's build in dictionary
So take a look a this:
>>> date = {}
>>> date['StartYear'] = 2016
>>> date['YourStartAge'] = 25
>>> date['LivExp'] = 80
>>> date
{'LivExp': 80, 'StartYear': 2016, 'YourStartAge': 25}
>>> # Accsess
>>> date['YourStartAge']
25
>>> date.get('YourStartAge')
25
>>> # get() can also be useful to catch stuff that are not in dict.
>>> date.get('hello', 'No in date dict')
'No in date dict'
So to this:
>>> YourStartAge = date['YourStartAge']
>>> YourStartAge
25
>>> # Now you have a varible YourStartAge
>>> # You have really moved it from your dict to Python build in dict(globals())
>>> globals()['YourStartAge']
25
Quote:3. What is the name of the file that the data is stored in?
It's up to you to figure out a name
The dictionary i just made in and out to disk with json.
import json
date = {'LivExp': 25, 'StartYear': 2016, 'YourStartAge': 25}
with open("my_file.json", "w") as j_in:
json.dump(date, j_in)
with open("my_file.json") as j_out:
saved_date = json.load(j_out)
print(saved_date) # {'LivExp': 80, 'YourStartAge': 25, 'StartYear': 2016}
So on disk it will be a json file,
json.load()
take it from disk an it will again be the original dict.