在嵌套循环中遍历多个变量

时间:2019-08-30 12:38:39

标签: python

我正在尝试遍历最近10天,并验证“帐户”是否在该时间段内位于第[0]行中。当我测试代码时,它将运行else语句而不是IF语句。我确保帐户位于表的第[0]行中,以便IF语句可以运行...但不能运行。

FYI第4行是我表中的日期列,因此我将其与变量i(最近10天)匹配

day_delta = datetime.timedelta(days=1)
qselectall = '''select account_number, proc_date, email_address, 
cy_day,to_char(load_date, 'YYYY-MM-DD') from sumb_email_conf'''
dupcursor.execute(qselectall)
start_date = datetime.date.today()
end_date = start_date + 7 * day_delta
for i in range((end_date - start_date).days):
    i = start_date - i * day_delta

df = DataFrame(accounts_sheet)
         email_address = df[df['cycle_day'] == 
         current_cycle_day].email_address_test
         account_numbers = df[df['cycle_day'] == 
         current_cycle_day].account_number
         for account, email in zip(account_numbers, email_address):
             print(account)
             for row in dupcursor.fetchall():
                 if str(i) in row[4] and account == row[0]:
                     print("Yes there are DUPS")
                     break
                 else:
                     print("No there are no DUPS")
                     break

1 个答案:

答案 0 :(得分:0)

@txemsukr由于else语句仍在运行,我必须做错了事。

day_delta = datetime.timedelta(days=1)
qselectall = '''select account_number, proc_date, email_address, 
cy_day,to_char(load_date, 'YYYY-MM-DD') from sumb_email_conf'''
dupcursor.execute(qselectall)
start_date = datetime.date.today()
end_date = start_date + 7 * day_delta
for i in range((end_date - start_date).days):
    i = start_date - i * day_delta


df = DataFrame(accounts_sheet)
    email_address = df[df['cycle_day'] == current_cycle_day].email_address_test
    account_numbers = df[df['cycle_day'] == current_cycle_day].account_number
    account = False
    for account, email in zip(account_numbers, email_address):
        for row in dupcursor.fetchall():
            if str(i) in row[4] and account == row[0]:
                account = True
        else:
            print("There are no dups")
            sql = '''INSERT INTO sumb_email_conf (proc_date, cy_day, account_number, email_address, load_date)
                                                                                    VALUES (%s,%s,%s,%s,%s)'''
            values = (yesterday, str(current_cycle_day), account, email,
                          str(datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")))
            insertcursor.execute(sql, values)
            connection.commit()
        print("There are Dups")