-
Notifications
You must be signed in to change notification settings - Fork 5
/
J.py
63 lines (47 loc) · 2.19 KB
/
J.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
import json
import requests
# JSON
# Create a dictionary
player_salaries = {'players': [{'position': 'RB', 'salary': 2300000}, {'position': 'QB', 'salary': 7400000}]}
# Standard dictionary, we can access individual components as expected
print(player_salaries)
print(player_salaries['players'])
# A solid understanding of navigating nested dicts/lists is important to working with JSON objects
# Specify dict keys and list indicies to narrow down to specific data
print(player_salaries['players'][0]['position'])
# You can loop through the list in this manner
for i in player_salaries['players']:
print(i['position'], i['salary'])
# Lets turn his dict into JSON
json_salaries = json.dumps(player_salaries)
# json.dumps converts the object into a string, which can be passed over the internet or to a database
print(json_salaries)
# These print exactly the same, so what's the difference?
# The first is still an object
# The second is now a string
print(player_salaries)
print(json_salaries)
# Write data to file
# First option attempts to write a dictionary to the file, which is an invalid operation
# but dumping it to the file as JSON work, because it converts the object to a string
with open('data.txt', 'w') as f:
# f.write(player_salaries)
json.dump(player_salaries, f)
# Read data from file
# This operation converts the string back into an object so we can work with the data
with open('data.txt') as f:
data = json.load(f)
# Proving that it is in object by specifying a key
print(data['players'])
# You can print your JSON in a more readable format
print(json.dumps(player_salaries, sort_keys=True, indent=4))
# Lets try to fetch some actual JSON data from a real API
url = 'https://tidesandcurrents.noaa.gov/api/datagetter?station=8722670&date=today&units=english&time_zone=lst&product=water_temperature&format=json'
r = requests.get(url).json()
# We can either use the pretty print function above, or a browser addon to prettify the JSON to make it easier to find
# individual components
# In this example we can find the first temperature reading with the following command
print(r['data'][0]['v'])
# We can iterage through every temperature reading like so
for i in r['data']:
print(i['v'])