How to not duplicate elements when entering array in csv

Question:

I have a piece of code used to enter student information. But I want to transform it so that Admission no. cannot be repeated, when entering an existing number, a message will be printed if you want to re-enter an existing number. below is my code

import csv

student_fields = ['Admission no.','Name','Age','Email','Phone']
student_database = 'students.csv'

def add_student():
    print("-------------------------")
    print("Add Student Information")
    print("-------------------------")
    global student_fields
    global student_database

    student_data = []
    for field in student_fields:
        print(field)
        value = input("Enter " + field + ": ")
        student_data.append(value)

    with open(student_database,"a",encoding = "utf-8") as f:
        writer = csv.writer(f)
        writer.writerows([student_data])

    print("Data saved successfully")
    input("Press any key to continue")
    return
Asked By: kukumanu32

||

Answers:

You can do something like this. As Barmar suggested, put the admission numbers in a set at the start of your definition. Then, check the user’s input against those numbers. Create a while loop that doesn’t let the user input any other value until they enter a new Admission no (and tell them that they are entering a duplicate admission number). Once everything looks good, write it to the csv.

import csv

student_fields = ['Admission no.','Name','Age','Email','Phone']
student_database = 'students.csv'

def add_student():
    print("-------------------------")
    print("Add Student Information")
    print("-------------------------")
    global student_fields
    global student_database
    
    # create a set of the already entered admission numbers
    with open('students.csv', 'r') as file:
        reader = csv.reader(file)
        admissionNums = {x[0] for x in reader if x}

    student_data = []
    for field in student_fields:
        print(field)
        value = input("Enter " + field + ": ")        
        
        # while the user is entering the admission no. field and they are entering a duplicate, keep prompting them for a new/unique number
        while field == 'Admission no.' and value in admissionNums:
            print("Admission no. already in file, try again")
            value = input("Enter " + field + ": ")
        student_data.append(value)

    # I also added `newline=''` so that it would stop creating an empty row when writing to the file
    # You can remove this if you want to keep making a new row every time you write to it
    with open(student_database,"a",encoding = "utf-8", newline='') as f:
        writer = csv.writer(f)
        writer.writerows([student_data])

    print("Data saved successfully")
    input("Press any key to continue")
    # The return statement here is not necessary
Answered By: Michael S.
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.