'Data redundancy on csv file

Hi Good day everyone I'm sorry I'm new to python programming sorry if I'm asking this even this is basic or not. Someone can help me with this? My problem is I want to write the data that capture's on my open cv Like for example "Name" "Reputation" or something else. And write it to my csv file. It writes But I has a redundancy like per capture it records The same name. I just want one Name that don't redundant Someone can Help me with this Please???

Here's the code to record data to csv:

def markattend(name, years, pos, tempe):
    with open('attendances.csv', 'r+') as f:
        datalist = f.readlines()
        nameList = []

        for line in datalist:
            entry = line.split(',')
            nameList.append(entry[0])

        if name and years and pos and tempe not in nameList:
            now = datetime.now()
            dtString = now.strftime('%I:%M:%a:%d:%b:%Y')
            f.writelines(f'\n{name},{years},{pos},{tempe},{dtString}')
        return datalist

And here I call the function and assigning its Data: like "Name" "Position"

if confidence>70:
    cv2.putText(img, datas, (x,y+205), cv2.FONT_HERSHEY_SIMPLEX, 0.8, color, 2, cv2.LINE_AA)
    cv2.putText(img, datas1, (x,y+230), cv2.FONT_HERSHEY_SIMPLEX, 0.8, color, 2, cv2.LINE_AA)
    cv2.putText(img, datas2, (x,y+250), cv2.FONT_HERSHEY_SIMPLEX, 0.8, color, 2, cv2.LINE_AA)
    cv2.putText(img, get_temp(ser), (x,y+280), cv2.FONT_HERSHEY_SIMPLEX, 0.8, color, 2, cv2.LINE_AA)
    markattend(datas,datas1,datas2,get_temp(ser))  # <== This temp is from my sensor data and datas, datas1, datas2   it correspond the "Name" "position" "Student"

Someone can help me plsss

Here's my csv file photo:here's the Image



Solution 1:[1]

  1. do not parse CSV manually:
import csv

data = []

with open('attendances.csv', newline='') as csvfile:
  reader = csv.reader(csvfile)
  for row in reader:
    data.append(row)
  1. ?
if name and years and pos and tempe not in nameList:

is the same as

if name is True and years is True and pos is True and tempe not in nameList:

correct code is:

import csv 

def markattend(name,years,pos,tempe):
    with open('attendances.csv','r+') as f:
    
        reader = csv.reader(f)
    
        for line in reader:
        
            if name == line[0] and years == line[1] and pos == line[2] and tempe == line[3]:
                ... do some stuff ...

Solution 2:[2]

for line in datalist:
    entry = line.split(',')
    nameList.append(entry[0])

Only the names are being added to the namesList say:

namesList = ['NameA', 'NameB']

The if statements used on your code does not work the way you think it does.

if 'NameC' and 'NameB' in namesList:

is going to evaluate it true, as you can see in the below snippet:

>>> namesList = ['NameA', 'NameB']
>>> 'NameC' and 'NameB' in namesList
True
>>> 'NameC' in namesList and 'NameB' in namesList
False

In your case, it is clear that tempe is not in the namesList, hence every entry is appended.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 rzlvmp
Solution 2 Jithin Johnson