update,
This commit is contained in:
48
vihuwach/task1/archive/task3.py
Normal file
48
vihuwach/task1/archive/task3.py
Normal file
@@ -0,0 +1,48 @@
|
||||
# https://api.data.gov.hk/v1/carpark-info-vacancy?data=vacancy&vehicleTypes=privateCar
|
||||
import requests
|
||||
from pprint import pprint
|
||||
|
||||
url = "https://api.data.gov.hk/v1/carpark-info-vacancy"
|
||||
params = {
|
||||
"data": "vacancy",
|
||||
"vehicleTypes": "privateCar"
|
||||
}
|
||||
|
||||
response = requests.get(url, params=params)
|
||||
js_result = response.json()['results']
|
||||
|
||||
output = []
|
||||
for js_row in js_result:
|
||||
if ((type(js_row.get('privateCar')) == type([]) ) and len(js_row.get('privateCar'))> 0):
|
||||
if (js_row.get('privateCar')[0].get("vacancy_type",'-')== "A"):
|
||||
output.append(js_row)
|
||||
|
||||
test_json = [{
|
||||
'park_Id': '10',
|
||||
'privateCar': [{
|
||||
'vacancy_type': 'A',
|
||||
'vacancy': 29,
|
||||
'lastupdate': '2024-10-18 16:04:59'
|
||||
}]
|
||||
}]
|
||||
|
||||
import pandas as pd
|
||||
|
||||
# Create a DataFrame from the test_json
|
||||
df = pd.json_normalize(test_json, record_path=['privateCar'], meta='park_Id')
|
||||
|
||||
|
||||
# Extract the 'vacancy' column and flatten it
|
||||
vacancy_df = df[['park_Id', 'vacancy']]
|
||||
|
||||
print(vacancy_df)
|
||||
|
||||
|
||||
pprint(output[0])
|
||||
pprint("length of json: "+str(len(output)))
|
||||
|
||||
import json
|
||||
|
||||
# Store output into task3_output.json
|
||||
with open('task3_output.json', 'w') as outfile:
|
||||
json.dump(output, outfile, indent=2)
|
Reference in New Issue
Block a user