Skip to content
Snippets Groups Projects
Commit 2d62714a authored by Andy Kuemmel's avatar Andy Kuemmel
Browse files

Update...

Update f22/andy_lec_notes/lec19_Oct21_JSON/.ipynb_checkpoints/lec19_json_template-checkpoint.ipynb, f22/andy_lec_notes/lec19_Oct21_JSON/.ipynb_checkpoints/lec_19_850-checkpoint.ipynb, f22/andy_lec_notes/lec19_Oct21_JSON/cs220_survey_data.csv, f22/andy_lec_notes/lec19_Oct21_JSON/kiva.json, f22/andy_lec_notes/lec19_Oct21_JSON/kiva_query.txt, f22/andy_lec_notes/lec19_Oct21_JSON/lec19_json_completed.ipynb, f22/andy_lec_notes/lec19_Oct21_JSON/lec19_json_template.ipynb, f22/andy_lec_notes/lec19_Oct21_JSON/lec19_worksheet_answers.ipynb, f22/andy_lec_notes/lec19_Oct21_JSON/readme.md, f22/andy_lec_notes/lec19_Oct21_JSON/score_history.json
parent ba33462f
No related branches found
No related tags found
No related merge requests found
Showing
with 1 addition and 1 deletion
%% Cell type:code id: tags: %% Cell type:code id: tags:
``` python ``` python
import csv import csv
import json # today's topic! import json # today's topic!
``` ```
%% Cell type:code id: tags: %% Cell type:code id: tags:
``` python ``` python
# Warmup 0: Recall how to read in the csv data # Warmup 0: Recall how to read in the csv data
# source: Automate the Boring Stuff with Python # source: Automate the Boring Stuff with Python
def process_csv(filename): def process_csv(filename):
exampleFile = open(filename, encoding="utf-8") exampleFile = open(filename, encoding="utf-8")
exampleReader = csv.reader(exampleFile) exampleReader = csv.reader(exampleFile)
exampleData = list(exampleReader) exampleData = list(exampleReader)
exampleFile.close() exampleFile.close()
return exampleData return exampleData
survey_data = process_csv('cs220_survey_data.csv') survey_data = process_csv('cs220_survey_data.csv')
header = None header = None
header header
``` ```
%% Cell type:code id: tags: %% Cell type:code id: tags:
``` python ``` python
# Warmup 1: Complete the cell function # Warmup 1: Complete the cell function
def cell(row_idx, col_name): def cell(row_idx, col_name):
col_idx = None col_idx = None
val = None val = None
if val == "": if val == "":
return None return None
elif col_name in ["Age"]: # used 'in' to allow for easy modification elif col_name in ["Age"]: # used 'in' to allow for easy modification
return int(val) return int(val)
else: else:
return val return val
cell(3, 'Pizza topping') cell(3, 'Pizza topping')
``` ```
%% Cell type:code id: tags: %% Cell type:code id: tags:
``` python ``` python
# Warmup 2: put survey_data into buckets by lecture # Warmup 2: put survey_data into buckets by lecture
# make a dictionary of lists # make a dictionary of lists
# key is the lecture # key is the lecture
rows = survey_data[1:] rows = survey_data[1:]
lecture_dict = {} lecture_dict = {}
for i in range(len(rows)): for i in range(len(rows)):
lecture = cell(i, 'Lecture') lecture = cell(i, 'Lecture')
# what goes here? # what goes here?
lecture_dict lecture_dict
``` ```
%% Output %% Output
--------------------------------------------------------------------------- ---------------------------------------------------------------------------
NameError Traceback (most recent call last) NameError Traceback (most recent call last)
<ipython-input-5-5bd8c0d3ae8b> in <module> <ipython-input-5-5bd8c0d3ae8b> in <module>
3 # key is the lecture 3 # key is the lecture
4 4
----> 5 rows = survey_data[1:] ----> 5 rows = survey_data[1:]
6 6
7 lecture_dict = {} 7 lecture_dict = {}
NameError: name 'survey_data' is not defined NameError: name 'survey_data' is not defined
%% Cell type:code id: tags: %% Cell type:code id: tags:
``` python ``` python
# Warmup 3: In lecture_dict, print out the key and the type of the value # Warmup 3: In lecture_dict, print out the key and the type of the value
``` ```
%% Cell type:code id: tags: %% Cell type:code id: tags:
``` python ``` python
# Warmup 4: Find the average age for our lecture # Warmup 4: Find the average age for our lecture
sum_of_ages = 0 sum_of_ages = 0
lecture_name = "LEC003" lecture_name = "LEC003"
num_people = 0 num_people = 0
for row in lecture_dict[lecture_name]: for row in lecture_dict[lecture_name]:
pass pass
average = round(sum_of_ages/num_people, 3) average = round(sum_of_ages/num_people, 3)
print(lecture_name, average) print(lecture_name, average)
``` ```
%% Output %% Output
--------------------------------------------------------------------------- ---------------------------------------------------------------------------
NameError Traceback (most recent call last) NameError Traceback (most recent call last)
<ipython-input-2-6a2163994cb9> in <module> <ipython-input-2-6a2163994cb9> in <module>
4 lecture_name = "LEC003" 4 lecture_name = "LEC003"
5 num_people = 0 5 num_people = 0
----> 6 for row in lecture_dict[lecture_name]: ----> 6 for row in lecture_dict[lecture_name]:
7 pass 7 pass
8 8
NameError: name 'lecture_dict' is not defined NameError: name 'lecture_dict' is not defined
%% Cell type:code id: tags: %% Cell type:code id: tags:
``` python ``` python
``` ```
%% Cell type:code id: tags: %% Cell type:code id: tags:
``` python ``` python
# Warmup 5: Make a dictionary of each lecture's average age # Warmup 5: Make a dictionary of each lecture's average age
# the key is the lecture name # the key is the lecture name
# the value is the average age # the value is the average age
# tabbing the copied code # tabbing the copied code
# first just print out all the results, # first just print out all the results,
# then add to a dictionary # then add to a dictionary
lec_age_dict = {} lec_age_dict = {}
for key in lecture_dict: for key in lecture_dict:
print(key) print(key)
# put code from above here # put code from above here
lec_age_dict lec_age_dict
``` ```
%% Output %% Output
LEC001 19.932 LEC001 19.932
LEC002 19.867 LEC002 19.867
LEC003 19.356 LEC003 19.356
LEC004 19.407 LEC004 19.407
LEC005 19.958 LEC005 19.958
{'LEC001': 19.932, {'LEC001': 19.932,
'LEC002': 19.867, 'LEC002': 19.867,
'LEC003': 19.356, 'LEC003': 19.356,
'LEC004': 19.407, 'LEC004': 19.407,
'LEC005': 19.958} 'LEC005': 19.958}
%% Cell type:code id: tags: %% Cell type:code id: tags:
``` python ``` python
``` ```
%% Cell type:code id: tags: %% Cell type:code id: tags:
``` python ``` python
``` ```
%% Cell type:code id: tags: %% Cell type:code id: tags:
``` python ``` python
``` ```
%% Cell type:markdown id: tags: %% Cell type:markdown id: tags:
## March 11: JSON Files ## Lecture 19: JSON Files
Learning Objectives: Learning Objectives:
- Interpret JSON formatted data and recognize differences between JSON and Python - Interpret JSON formatted data and recognize differences between JSON and Python
- Deserialize data from JSON for use in Python programs (read) - Deserialize data from JSON for use in Python programs (read)
- Serialize data into JSON for long term storage (write) - Serialize data into JSON for long term storage (write)
%% Cell type:code id: tags: %% Cell type:code id: tags:
``` python ``` python
# We will be looking at lecture slides to understand JSON # We will be looking at lecture slides to understand JSON
``` ```
%% Cell type:code id: tags: %% Cell type:code id: tags:
``` python ``` python
# Deserialize # Deserialize
def read_json(path): def read_json(path):
with open(path, encoding="utf-8") as f: # f is a varaible with open(path, encoding="utf-8") as f: # f is a varaible
return json.load(f) # f represents a reference the JSON file return json.load(f) # f represents a reference the JSON file
# Serialize # Serialize
def write_json(path, data): def write_json(path, data):
with open(path, 'w', encoding="utf-8") as f: with open(path, 'w', encoding="utf-8") as f:
json.dump(data, f, indent=2) json.dump(data, f, indent=2)
``` ```
%% Cell type:code id: tags: %% Cell type:code id: tags:
``` python ``` python
# first, let's take a look at the file score_history.json # first, let's take a look at the file score_history.json
``` ```
%% Cell type:code id: tags: %% Cell type:code id: tags:
``` python ``` python
# now let's read it in and investigate the data # now let's read it in and investigate the data
scores_dict = read_json('score_history.json') scores_dict = read_json('score_history.json')
#print(type(scores_dict)) #print(type(scores_dict))
#print(scores_dict.keys()) #print(scores_dict.keys())
#print(scores_dict['bob']) #print(scores_dict['bob'])
#print(scores_dict) #print(scores_dict)
#scores_dict['andy'] = [17.0, 84.0] #scores_dict['andy'] = [17.0, 84.0]
#print(scores_dict) #print(scores_dict)
``` ```
%% Output %% Output
<class 'dict'> <class 'dict'>
dict_keys(['bob', 'alice', 'meena']) dict_keys(['bob', 'alice', 'meena'])
[20.0, 10.0] [20.0, 10.0]
{'bob': [20.0, 10.0], 'alice': [30.0, 20.0], 'meena': [100.0, 10.0]} {'bob': [20.0, 10.0], 'alice': [30.0, 20.0], 'meena': [100.0, 10.0]}
{'bob': [20.0, 10.0], 'alice': [30.0, 20.0], 'meena': [100.0, 10.0], 'andy': [17.0, 84.0]} {'bob': [20.0, 10.0], 'alice': [30.0, 20.0], 'meena': [100.0, 10.0], 'andy': [17.0, 84.0]}
%% Cell type:code id: tags: %% Cell type:code id: tags:
``` python ``` python
# Let's practice writing to a JSON file # Let's practice writing to a JSON file
# when I'm testing code, I like to name my output file differently from my input file # when I'm testing code, I like to name my output file differently from my input file
# so that I don't accidentally erase or overwrite my data # so that I don't accidentally erase or overwrite my data
write_json('score_history2.json', scores_dict) write_json('score_history2.json', scores_dict)
``` ```
%% Cell type:markdown id: tags: %% Cell type:markdown id: tags:
### We can make JSON files in many varied ways ### We can make JSON files in many varied ways
### This makes a list of dictionaries ### This makes a list of dictionaries
%% Cell type:code id: tags: %% Cell type:code id: tags:
``` python ``` python
# Code from last lecture, # Code from last lecture,
# reads in the survey data into a list of dicts # reads in the survey data into a list of dicts
table_dict_list = [] table_dict_list = []
for i in range(len(rows)): for i in range(len(rows)):
row = rows[i] row = rows[i]
row_dict = {} row_dict = {}
for item in header: # iterate through each column name for item in header: # iterate through each column name
row_dict[item] = row[header.index(item)] # find the value in the row using .index row_dict[item] = row[header.index(item)] # find the value in the row using .index
print(row_dict) print(row_dict)
# add row_dict to table_dict_list # add row_dict to table_dict_list
table_dict_list # what is this? A ______ of _________ table_dict_list # what is this? A ______ of _________
``` ```
%% Output %% Output
--------------------------------------------------------------------------- ---------------------------------------------------------------------------
NameError Traceback (most recent call last) NameError Traceback (most recent call last)
<ipython-input-10-ebdc3b3869b4> in <module> <ipython-input-10-ebdc3b3869b4> in <module>
2 # reads in the survey data into a list of dicts 2 # reads in the survey data into a list of dicts
3 table_dict_list = [] 3 table_dict_list = []
----> 4 for i in range(len(rows)): ----> 4 for i in range(len(rows)):
5 row = rows[i] 5 row = rows[i]
6 row_dict = {} 6 row_dict = {}
NameError: name 'rows' is not defined NameError: name 'rows' is not defined
%% Cell type:code id: tags: %% Cell type:code id: tags:
``` python ``` python
# Let's write this list of dictionaries into a JSON file # Let's write this list of dictionaries into a JSON file
write_json('cs220_as_json_list.json', table_dict_list) write_json('cs220_as_json_list.json', table_dict_list)
``` ```
%% Cell type:code id: tags: %% Cell type:code id: tags:
``` python ``` python
# Verify: can you find this file in your directory? # Verify: can you find this file in your directory?
``` ```
%% Cell type:code id: tags: %% Cell type:code id: tags:
``` python ``` python
# let's write our original dictionary of lists - buckets into a JSON file # let's write our original dictionary of lists - buckets into a JSON file
write_json('cs220_as_json_dict.json', lecture_dict) write_json('cs220_as_json_dict.json', lecture_dict)
``` ```
%% Cell type:code id: tags: %% Cell type:code id: tags:
``` python ``` python
# Verify: can you find this file in your directory? # Verify: can you find this file in your directory?
``` ```
%% Cell type:markdown id: tags: %% Cell type:markdown id: tags:
### Many Web Sites have APIs that allow you to get their data ### Many Web Sites have APIs that allow you to get their data
### Kiva.com Micro-lending site ### Kiva.com Micro-lending site
%% Cell type:code id: tags: %% Cell type:code id: tags:
``` python ``` python
# Take a look at kiva.json # Take a look at kiva.json
# read it into a dictionary # read it into a dictionary
kiva_dict = read_json('kiva.json') kiva_dict = read_json('kiva.json')
kiva_dict kiva_dict
``` ```
%% Output %% Output
--------------------------------------------------------------------------- ---------------------------------------------------------------------------
NameError Traceback (most recent call last) NameError Traceback (most recent call last)
<ipython-input-12-2f403470e659> in <module> <ipython-input-12-2f403470e659> in <module>
2 2
3 # read it into a dictionary 3 # read it into a dictionary
----> 4 kiva_dict = read_json('kiva.json') ----> 4 kiva_dict = read_json('kiva.json')
5 kiva_dict 5 kiva_dict
NameError: name 'read_json' is not defined NameError: name 'read_json' is not defined
%% Cell type:code id: tags: %% Cell type:code id: tags:
``` python ``` python
# Plumbing the data # Plumbing the data
kiva_dict['data'] # this gives us a list of dicts kiva_dict['data'] # this gives us a list of dicts
loan_list = None loan_list = None
loan_list loan_list
``` ```
%% Output %% Output
--------------------------------------------------------------------------- ---------------------------------------------------------------------------
NameError Traceback (most recent call last) NameError Traceback (most recent call last)
<ipython-input-11-07e7d0c7ba46> in <module> <ipython-input-11-07e7d0c7ba46> in <module>
1 # Plumbing the data 1 # Plumbing the data
----> 2 kiva_dict['data'] # this gives us a list of dicts ----> 2 kiva_dict['data'] # this gives us a list of dicts
3 loan_list = None 3 loan_list = None
4 loan_list 4 loan_list
NameError: name 'kiva_dict' is not defined NameError: name 'kiva_dict' is not defined
%% Cell type:code id: tags: %% Cell type:code id: tags:
``` python ``` python
# what can we learn from this data? # what can we learn from this data?
for loan_dict in loan_list: for loan_dict in loan_list:
print(type(loan)) print(type(loan))
for key in loan_dict: for key in loan_dict:
print(key) print(key)
``` ```
%% Output %% Output
<class 'dict'> <class 'dict'>
name name
description description
loanAmount loanAmount
geocode geocode
<class 'dict'> <class 'dict'>
name name
description description
loanAmount loanAmount
geocode geocode
<class 'dict'> <class 'dict'>
name name
description description
loanAmount loanAmount
geocode geocode
<class 'dict'> <class 'dict'>
name name
description description
loanAmount loanAmount
geocode geocode
<class 'dict'> <class 'dict'>
name name
description description
loanAmount loanAmount
geocode geocode
%% Cell type:code id: tags: %% Cell type:code id: tags:
``` python ``` python
# print out all the names # print out all the names
for loan_dict in loan_list: for loan_dict in loan_list:
print(loan_dict) print(loan_dict)
``` ```
%% Output %% Output
--------------------------------------------------------------------------- ---------------------------------------------------------------------------
NameError Traceback (most recent call last) NameError Traceback (most recent call last)
<ipython-input-13-086b83d4d2f9> in <module> <ipython-input-13-086b83d4d2f9> in <module>
1 # print out all the names 1 # print out all the names
----> 2 for loan_dict in loan_list: ----> 2 for loan_dict in loan_list:
3 print(loan_dict) 3 print(loan_dict)
NameError: name 'loan_list' is not defined NameError: name 'loan_list' is not defined
%% Cell type:code id: tags: %% Cell type:code id: tags:
``` python ``` python
# print out all the country names # print out all the country names
for loan_dict in loan_list: for loan_dict in loan_list:
print(loan_dict) print(loan_dict)
``` ```
%% Output %% Output
{'name': 'Albania', 'region': 'Eastern Europe', 'fundsLentInCountry': 9051250} {'name': 'Albania', 'region': 'Eastern Europe', 'fundsLentInCountry': 9051250}
{'name': 'Tajikistan', 'region': 'Asia', 'fundsLentInCountry': 64243075} {'name': 'Tajikistan', 'region': 'Asia', 'fundsLentInCountry': 64243075}
{'name': 'Kenya', 'region': 'Africa', 'fundsLentInCountry': 120841775} {'name': 'Kenya', 'region': 'Africa', 'fundsLentInCountry': 120841775}
{'name': 'Kenya', 'region': 'Africa', 'fundsLentInCountry': 120841775} {'name': 'Kenya', 'region': 'Africa', 'fundsLentInCountry': 120841775}
{'name': 'Togo', 'region': 'Africa', 'fundsLentInCountry': 13719125} {'name': 'Togo', 'region': 'Africa', 'fundsLentInCountry': 13719125}
%% Cell type:code id: tags: %% Cell type:code id: tags:
``` python ``` python
# go to the kiva.com site and experiement with their API # go to the kiva.com site and experiement with their API
# https://www.kiva.org/build # https://www.kiva.org/build
``` ```
%% Cell type:markdown id: tags: %% Cell type:markdown id: tags:
## After Lecture: Worksheet ## After Lecture: Worksheet
%% Cell type:code id: tags: %% Cell type:code id: tags:
``` python ``` python
``` ```
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment