简体   繁体   中英

opencv attendance system mark student as present in csv problem

i have an attendee system made with opencv, it works but my problem is with mark student in csv file, the goal is to mark student only once a day

///////// after reading more about csv ive found that my logic was bad.

here is the latest code to solve my problem

def markAttendance(name):
today = datetime.now().strftime("%d-%m-%Y")
if not path.exists('opencv/prezente/'+today+'.csv'):
    with open('opencv/prezente/'+today+'.csv', 'w') as file:
        file.write(f'{"nume"},{"timestamp"}')

    with open('opencv/prezente/'+today+'.csv', 'r+') as f:
        now = datetime.now().strftime("%d/%m/%Y,%H:%M")
        f.write(f'\n{name},{now}')
else:
    with open('opencv/prezente/'+today+'.csv', 'r+') as f:
        myDataList = f.readlines()
        row_count = sum(1 for row in myDataList)
        exista = []
        for line in myDataList:
            if name not in line:
                exista.append(name)
        if row_count == len(exista):
            now = datetime.now().strftime("%d/%m/%Y,%H:%M")
            f.write(f'\n{name},{now}')

so here is my markattendance function

def markAttendance(name):
  with open('opencv/attendance.csv', 'r+') as f:
    myDataList = f.readlines()
    for line in myDataList:
        entry = line.split(',')
        today = datetime.now().strftime("%d/%m/%Y")
       
        if name in line and entry[1] == today:
            print(entry[1]+" ai mai fost azi " + entry[2])  
            
           
        else:
            now = datetime.now().strftime("%d/%m/%Y,%H:%M")
            # f.write(f'\n{name},{now}')
            print("ciubaca")

i have comented the line f.write because there is my problem, from my logic that part should not execute if the condition is true but instead this is what i get in console

在此处输入图像描述

What exactly is the problem you want to fix? My hunch is that you need an if elif statement instead of if else because right now the else statement could be triggering either if the name is not in the line or if entry[1] is not today:

def markAttendance(name):
    with open('opencv/attendance.csv', 'r+') as f:
        myDataList = f.readlines()
        for line in myDataList:
            entry = line.split(',')
            today = datetime.now().strftime("%d/%m/%Y")
   
            if name in line and entry[1] == today:
                print(entry[1]+" ai mai fost azi " + entry[2])  
        
            elif name not in line and entry[1] == today:
                now = datetime.now().strftime("%d/%m/%Y,%H:%M")
                # f.write(f'\n{name},{now}')
                print("ciubaca")

But I'd need more details on the problem to be sure.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM