def store_all_candidates(): def get_county(constituency): if constituency[2:5] == "原住民": return "原住民" else: return constituency[0:3] query = ManualCandidate.select() candidates = [{ "county": get_county(candidate.constituency), "constituency": candidate.constituency, "name": candidate.name, "party": candidate.party, "photo": candidate.photo, "experience": candidate.experience, "currentLegislator": candidate.currentLegislator, } for candidate in query] with open("../data/static/regional_candidates.json", "w") as fp: json.dump(candidates, fp, ensure_ascii=False, indent=2)
df = pd.read_csv("../data/manual/2020candidate_regional.csv") df.columns = [ "constituency", # 選區 "name", # 姓名 "fbPage", # FB 粉絲專頁 "fbPersonalPage", # FB 粉絲專頁 "photo", # 照片 "party", # 政黨 "currentLegislator", # 現任立委 "beenLegislator", # 曾任立委 "dayOfBirth", # 生日 "age", # 年齡 "wiki", # wiki "education", # 學歷 "educationConn", # 學歷連結 "experience", # 經歷 "experienceConn", # 經歷連結 "politics", # 政見 "politicsConn", # 政見連結 "other", ] data = df.replace({np.nan: None}).to_dict(orient="records") data = [{ **d, "currentLegislator": True if d["currentLegislator"] == "Y" else False, "beenLegislator": True if d["beenLegislator"] == "Y" else False, } for d in data] ManualCandidate.drop_table() ManualCandidate.create_table() ManualCandidate.insert_many(data).execute()