Files
004_comission/vihuwach/task1/archive/task2d.py
louiscklaw 6c95c5e3ea update,
2025-01-31 21:57:33 +08:00

95 lines
2.7 KiB
Python

import pandas as pd
import json
import datetime
from pprint import pprint
test_json = []
# Load and parse the JSON file
with open('js_result.json', 'r') as file:
test_json = json.load(file)
ph_dates = [
'2024-10-18',
]
task1_json = []
# Load and parse the JSON file
with open('task1.json', 'r') as file:
task1_json = json.load(file)
def get_url_image(js_row):
url_image = ''
return None if js_row.get('renditionUrls', {}).get("square") == None else js_row['renditionUrls']['square']
task1_df = pd.DataFrame(task1_json)
def get_todayinfo(js_row, ph_dates, today_date_str = datetime.datetime.today().strftime('%Y-%m-%d')):
given_date = datetime.datetime.strptime(today_date_str, '%Y-%m-%d')
weekdays = ['MON', 'TUE', 'WED', 'THU', 'FRI', 'SAT', 'SUN']
txt_weekday = "PH" if today_date_str in ph_dates else weekdays[given_date.weekday()]
output = {
'periodStart': '',
'periodEnd': '',
'price': 0,
'space': 0,
'today': today_date_str,
'today_weekday': txt_weekday
}
if (js_row.get('privateCar') != None):
jp = js_row['privateCar']
if (jp.get('space') != None):
output['space'] = js_row['privateCar']['space']
if (jp.get('hourlyCharges') != None):
for i in js_row['privateCar']['hourlyCharges']:
if txt_weekday in i['weekdays']:
output['periodStart'] = i['periodStart']
output['periodEnd'] = i['periodEnd']
output['price'] = i['price']
break
output['today'] = today_date_str
output['today_weekday'] = txt_weekday
return output
hodgepodge = []
for jr in task1_json:
hodgepodge.append({**jr, **get_todayinfo(jr, ph_dates), "url_image": get_url_image(jr)})
output = []
for hp in hodgepodge:
output.append({
"park_Id": hp.get('park_Id',""),
"name": hp.get('name',""),
"displayAddress": hp.get('displayAddress',""),
"district": hp.get('district',""),
"latitude": hp.get('latitude',""),
"longitude": hp.get('longitude',""),
"opening_status": hp.get('opening_status',""),
"facilities": hp.get('facilities',"") ,
"paymentMethods": hp.get('paymentMethods',"") ,
"modifiedDate": hp.get('modifiedDate',""),
'periodStart': hp.get('periodStart',""),
"url_image": hp.get('url_image',""),
'periodEnd': hp.get('periodEnd',""),
'price': hp.get('price',""),
'space': hp.get('space',""),
'today': hp.get('today',""),
'today_weekday': hp.get('today_weekday',""),
})
pprint(output[0])
# Translate the JSON data to a pandas DataFrame
test_df = pd.DataFrame(test_json)
key_col = 'park_Id'
new_df = test_df[[key_col, 'col2', 'col3']]
print("done")