Skip to content

Commit fab06d0

Browse files
Corey SchaferCorey Schafer
Corey Schafer
authored and
Corey Schafer
committed
Python JSON Snippets
1 parent fc5e8dd commit fab06d0

File tree

4 files changed

+487
-0
lines changed

4 files changed

+487
-0
lines changed

Python-JSON/api.py

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import json
2+
from urllib.request import urlopen
3+
4+
with urlopen("https://finance.yahoo.com/webservice/v1/symbols/allcurrencies/quote?format=json") as response:
5+
source = response.read()
6+
7+
data = json.loads(source)
8+
9+
# print(json.dumps(data, indent=2))
10+
11+
usd_rates = dict()
12+
13+
for item in data['list']['resources']:
14+
name = item['resource']['fields']['name']
15+
price = item['resource']['fields']['price']
16+
usd_rates[name] = price
17+
18+
print(50 * float(usd_rates['USD/INR']))

Python-JSON/json_demo.py

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
''' JavaScript Object Notation '''
2+
import json
3+
4+
with open('states.json') as f:
5+
data = json.load(f)
6+
7+
for state in data['states']:
8+
del state['area_codes']
9+
10+
with open('new_states.json', 'w') as f:
11+
json.dump(data, f, indent=2)

Python-JSON/new_states.json

+204
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,204 @@
1+
{
2+
"states": [
3+
{
4+
"name": "Alabama",
5+
"abbreviation": "AL"
6+
},
7+
{
8+
"name": "Alaska",
9+
"abbreviation": "AK"
10+
},
11+
{
12+
"name": "Arizona",
13+
"abbreviation": "AZ"
14+
},
15+
{
16+
"name": "Arkansas",
17+
"abbreviation": "AR"
18+
},
19+
{
20+
"name": "California",
21+
"abbreviation": "CA"
22+
},
23+
{
24+
"name": "Colorado",
25+
"abbreviation": "CO"
26+
},
27+
{
28+
"name": "Connecticut",
29+
"abbreviation": "CT"
30+
},
31+
{
32+
"name": "Delaware",
33+
"abbreviation": "DE"
34+
},
35+
{
36+
"name": "Florida",
37+
"abbreviation": "FL"
38+
},
39+
{
40+
"name": "Georgia",
41+
"abbreviation": "GA"
42+
},
43+
{
44+
"name": "Hawaii",
45+
"abbreviation": "HI"
46+
},
47+
{
48+
"name": "Idaho",
49+
"abbreviation": "ID"
50+
},
51+
{
52+
"name": "Illinois",
53+
"abbreviation": "IL"
54+
},
55+
{
56+
"name": "Indiana",
57+
"abbreviation": "IN"
58+
},
59+
{
60+
"name": "Iowa",
61+
"abbreviation": "IA"
62+
},
63+
{
64+
"name": "Kansas",
65+
"abbreviation": "KS"
66+
},
67+
{
68+
"name": "Kentucky",
69+
"abbreviation": "KY"
70+
},
71+
{
72+
"name": "Louisiana",
73+
"abbreviation": "LA"
74+
},
75+
{
76+
"name": "Maine",
77+
"abbreviation": "ME"
78+
},
79+
{
80+
"name": "Maryland",
81+
"abbreviation": "MD"
82+
},
83+
{
84+
"name": "Massachusetts",
85+
"abbreviation": "MA"
86+
},
87+
{
88+
"name": "Michigan",
89+
"abbreviation": "MI"
90+
},
91+
{
92+
"name": "Minnesota",
93+
"abbreviation": "MN"
94+
},
95+
{
96+
"name": "Mississippi",
97+
"abbreviation": "MS"
98+
},
99+
{
100+
"name": "Missouri",
101+
"abbreviation": "MO"
102+
},
103+
{
104+
"name": "Montana",
105+
"abbreviation": "MT"
106+
},
107+
{
108+
"name": "Nebraska",
109+
"abbreviation": "NE"
110+
},
111+
{
112+
"name": "Nevada",
113+
"abbreviation": "NV"
114+
},
115+
{
116+
"name": "New Hampshire",
117+
"abbreviation": "NH"
118+
},
119+
{
120+
"name": "New Jersey",
121+
"abbreviation": "NJ"
122+
},
123+
{
124+
"name": "New Mexico",
125+
"abbreviation": "NM"
126+
},
127+
{
128+
"name": "New York",
129+
"abbreviation": "NY"
130+
},
131+
{
132+
"name": "North Carolina",
133+
"abbreviation": "NC"
134+
},
135+
{
136+
"name": "North Dakota",
137+
"abbreviation": "ND"
138+
},
139+
{
140+
"name": "Ohio",
141+
"abbreviation": "OH"
142+
},
143+
{
144+
"name": "Oklahoma",
145+
"abbreviation": "OK"
146+
},
147+
{
148+
"name": "Oregon",
149+
"abbreviation": "OR"
150+
},
151+
{
152+
"name": "Pennsylvania",
153+
"abbreviation": "PA"
154+
},
155+
{
156+
"name": "Rhode Island",
157+
"abbreviation": "RI"
158+
},
159+
{
160+
"name": "South Carolina",
161+
"abbreviation": "SC"
162+
},
163+
{
164+
"name": "South Dakota",
165+
"abbreviation": "SD"
166+
},
167+
{
168+
"name": "Tennessee",
169+
"abbreviation": "TN"
170+
},
171+
{
172+
"name": "Texas",
173+
"abbreviation": "TX"
174+
},
175+
{
176+
"name": "Utah",
177+
"abbreviation": "UT"
178+
},
179+
{
180+
"name": "Vermont",
181+
"abbreviation": "VT"
182+
},
183+
{
184+
"name": "Virginia",
185+
"abbreviation": "VA"
186+
},
187+
{
188+
"name": "Washington",
189+
"abbreviation": "WA"
190+
},
191+
{
192+
"name": "West Virginia",
193+
"abbreviation": "WV"
194+
},
195+
{
196+
"name": "Wisconsin",
197+
"abbreviation": "WI"
198+
},
199+
{
200+
"name": "Wyoming",
201+
"abbreviation": "WY"
202+
}
203+
]
204+
}

0 commit comments

Comments
 (0)