1

I am working on one program and trying to achieve following functionalities.

  • add new student
  • Remove student based on id

here is my code

from csv import writer
import csv

def add(file_name, list_of_elem):
    # Open file in append mode
    with open(file_name, 'a+', newline='') as write_obj:
        # Create a writer object from csv module
        csv_writer = writer(write_obj)
        # Add contents of list as last row in the csv file
        csv_writer.writerow(list_of_elem)

def remove():
    id = input("Enter ID : ")
    with open('students.csv', 'rb') as inp, open('students.csv', 'wb') as out:
        writer = csv.writer(out)
        for row in csv.reader(inp):
            if row[0] != id:
                writer.writerow(row)


# List of strings
row_contents = [11,'mayur','Java','Tokyo','Morning']
# Append a list as new line to an old csv file
add('students.csv', row_contents)
remove()

add function works properly but when i tried remove function it removes all existing entries.Could anyone please help me.

Mayur Satav
  • 985
  • 2
  • 12
  • 32

2 Answers2

0

First I will show the code and below I will left some comments about the changes.

from csv import writer
import csv

def add(file_name, list_of_elem):
    # Open file in append mode
    with open(file_name, 'a+', newline = '') as write_obj:
        # Create a writer object from csv module
        csv_writer = writer(write_obj)
        # Add contents of list as last row in the csv file
        csv_writer.writerow(list_of_elem)

def remove():
    idt = input("Enter ID : ")
    with open('students.csv', 'r') as inp:
        newrows = []
        data = csv.reader(inp)
        for row in data:
            if row[0] != idt:
                newrows.append(row)
    with open('students.csv', 'w') as out:
        csv_writer = writer(out)
        for row in newrows:
            csv_writer.writerow(row)

def display():
    with open('students.csv','r') as f:
        data = csv.reader(f)
        for row in data:
                print(row)

# List of strings
row_contents = [10,'mayur','Java','Tokyo','Morning']
add('students.csv', row_contents)
row_contents = [11,'mayur','Java','Tokyo','Morning']
add('students.csv', row_contents)
row_contents = [12,'mayur','Java','Tokyo','Morning']
add('students.csv', row_contents)
# Append a list as new line to an old csv file

display()
remove()
  1. If your file is a CSV, you should use a text file, instead of a binary one.
  2. I changed the name of the variable id to ìdt because id is built-in to return the identity of an object and it's not a good practice overwrite built-in functions.
  3. To remove only rows with an specific idt you should read all the file, store into a var (list), remove what you want to delete and only after that save the result.
j3r3mias
  • 365
  • 2
  • 12
0

You should use a temporary file instead of opening and writing to the same file simultaneously. Checkout this answer: https://stackoverflow.com/a/17646958/14039323

pacuna
  • 1,831
  • 16
  • 15