-
Notifications
You must be signed in to change notification settings - Fork 0
/
Params_to_csv.py
53 lines (41 loc) · 1.33 KB
/
Params_to_csv.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
# -*- coding: utf-8 -*-
"""
Created on Thu Jun 11 18:48:00 2020
@author: GeovaniBM
"""
import json
import csv
import requests
path = "C:/Users/Issstezac1/Desktop"
endpoint = "https://api.data.gov.sg/v1/transport/traffic-images"
"""response from API URL"""
get_req = requests.get(endpoint)
"""Transform json input to python objects"""
data = json.loads(get_req.text)
##Prints all the json data
##print(data.get('items'))
def extract_values(obj, key):
"""Pull all values of specified key from nested JSON."""
arr = []
def extract(obj, arr, key):
"""Recursively search for values of key in JSON tree."""
if isinstance(obj, dict):
for k, v in obj.items():
if isinstance(v, (dict, list)):
extract(v, arr, key)
elif k == key:
arr.append(v)
elif isinstance(obj, list):
for item in obj:
extract(item, arr, key)
return arr
results = extract(obj, arr, key)
return results
"""list of parameters"""
cam_list = extract_values(data,'camera_id')
images_list = extract_values(data, 'image')
"""writing data list into csv file """
with open('C:/Users/Issstezac1/Desktop/test.csv', 'w+', newline='') as myfile:
wr = csv.writer(myfile)
wr.writerow(cam_list)
wr.writerow(images_list)