def run(*args): try: df = pd.read_csv("data/barinfo.csv") good_site = set() for index, row in df.iterrows(): try: Site.objects.create(site_id=row["SITEID"], title=row["TITLE"], lng=row["lng"], lat=row["lat"]) good_site.add(str(row["SITEID"])) except: pass print(len(good_site)) for data_file in os.listdir("data"): if data_file.startswith("hydata"): df = pd.read_csv("data/%s" % data_file) data = [] for index, row in df.iterrows(): try: r = Record() r.person_id = row["PERSONID"] r.site_id = str(row["SITEID"]) r.is_female = row["XB"] == "女" r.customer_name = row["CUSTOMERNAME"] r.online_time = get_datetime(row["ONLINETIME"]) r.offline_time = get_datetime(row["OFFLINETIME"]) r.area_id = row["AREAID"] r.birthday = get_date(row["BIRTHDAY"]) if r.person_id is None or r.site_id is None or r.is_female is None or r.customer_name is None or \ r.online_time is None or r.offline_time is None or r.area_id is None or r.birthday is None: continue # print("MID") if r.site_id not in good_site: # print(r.site_id) continue data.append(r) if len(data) >= 1000: Record.objects.bulk_create(data) data = [] print("ok!") except: pass Record.objects.bulk_create(data) except: traceback.print_exc()
def make_time(user_id, date_id, day): s = datetime.time(randint(15, 18), 0, 0) e = datetime.time(randint(19, 22), 0, 0) start = datetime.datetime.combine(day, s) end = datetime.datetime.combine(day, e) return Record(user_id=user_id, date_id=date_id, start=start, end=end)
def make_record(): data = [] for date in tqdm(Date.query.all()): data = [] #members = 0 users = [] for user in choose_users(): if user is None: continue else: user = User.query.filter_by(name=user).first() time = make_time(date.day) record = Record(start=time[0], end=time[0], user=user, date=date) users.append(record) data.append(record) #members += 1 date.user_date = users data.append(date) db.session.add_all(data) db.session.commit()