A document from MCS 275 Spring 2021, instructor David Dumas. You can also get the notebook file.

CSV

In [2]:
import csv
In [3]:
# csv.reader interface
po_names = set()

with open("data/ri.csv","rt",newline="") as csvfile:
    rdr = csv.reader(csvfile)
    for row in rdr:
        name = row[3]  # ??? what is this?
        if name:
            po_names.add(name)
In [4]:
# csv.DictReader interface
po_names = set()

with open("data/ri.csv","rt",newline="") as csvfile:
    rdr = csv.DictReader(csvfile)
    for row in rdr:
        name = row["PO Name"]  # much clearer
        if name:
            po_names.add(name)
In [12]:
po_names
Out[12]:
{'BARRINGTON',
 'COVENTRY',
 'HARRISVILLE',
 'NEWPORT',
 'NORTH KINGSTOWN',
 'PASCOAG',
 'PAWTUCKET',
 'PORTSMOUTH',
 'PROVIDENCE',
 'WAKEFIELD',
 'WARREN',
 'WARWICK',
 'WEST WARWICK',
 'WESTERLY',
 'WOONSOCKET'}

JSON

In [5]:
import json
In [6]:
with open("data/nasa_earth_assets.json","rt") as jsonfile:
    val = json.load(jsonfile)
In [7]:
type(val)
Out[7]:
dict
In [8]:
val["resource"]
Out[8]:
{'dataset': 'LANDSAT/LC08/C01/T1_SR', 'planet': 'earth'}
In [11]:
# Check that this is terrestrial imagery
val["resource"]["planet"] == "earth"
Out[11]:
True
In [ ]:
# The lines below are not expected to work, but they are the kinds
# of queries you might use to explore an object returned from
# reading a JSON file.
d["metadata"]["date"]
d["metadata"]["version"]
d["response"]["scores"]["quiz7"]