1

I have a piece of code where I am trying to gain the user's input and delete the row of that specific line in the csv file. I have tired doing so with the code below but it deletes the row and header too. I want to just delete the row not the header, the problem is in the remove_a_team function. How can I fix this? Thank you.

import csv
import time

header_teams = ['TeamName', 'Member1', 'Member2', 'Member3', 'Member4', 'Member5']

#creating a header for the team's csv file.
with open('teams.csv', 'w', newline = '', encoding='UTF8') as f:
    writer = csv.writer(f)
    #writing the header for the csv file.
    writer.writerow(header_teams)
f.close()

def add_a_team():
    team_adder = []
    name = str(input("You are now entering the data for a team.\nEnter the team's name."))
    team_adder.append(name)
    time.sleep(0.3)
    name = str(input("Enter the first team member's name."))
    team_adder.append(name)
    time.sleep(0.3)
    name = str(input("Enter the second team member's name."))
    team_adder.append(name)
    time.sleep(0.3)
    name = str(input("Enter the third team member's name."))
    team_adder.append(name)
    time.sleep(0.3)
    name = str(input("Enter the fourth team member's name."))
    team_adder.append(name)
    time.sleep(0.3)
    name = str(input("Enter the fifth team member's name."))
    team_adder.append(name)
    time.sleep(0.3)
    print("Team successfully added.")
    print("\n")
    with open('teams.csv', 'a', newline = '', encoding='UTF8') as f:
        writer = csv.writer(f)
        #writing the data for each team.
        writer.writerow(team_adder)
    f.close()

def remove_a_team():
    content_team = input(str("What team would you like to remove?"))
    with open('teams.csv', 'r', newline = '', encoding='UTF8') as f:
        lines = f.readlines()
                         
    with open('teams.csv', 'w', newline = '', encoding='UTF8') as f:
        for line in lines:
            if line == content_team:
                writer.writerow(row)
            else:
                print("That team does not exist")
    f.close()

add_a_team()
remove_a_team()

1 Answer 1

2

Guessing that content_team = input(str("What team would you like to remove?")) should give the name of the team you wish to delete, then in this block:

with open('teams.csv', 'w', newline = '', encoding='UTF8') as f:
        for line in lines:
            if line == content_team:
                writer.writerow(line)
            else:
                print("That team does not exist")

The variable row does not exist. I am guessing it is supposed to be line and that you intended to write to f. you have the logic inverted. you are writing the row if the team name matches, but want it the other way around. Additionally you are checking the entire line against the content_team instead of only the first field. The whole function should be:

def remove_a_team():
    content_team = input(str("What team would you like to remove?"))
    with open('teams.csv', 'r', newline = '', encoding='UTF8') as f:
        lines = f.readlines()
                         
    found_team=False
    with open('teams2.csv', 'w', newline = '', encoding='UTF8') as f:
        for line in lines:
            if line.split(',')[0] == content_team:
                found_team=True
            else:
                f.write(line)
    if not found_team:                
        print("That team does not exist")
    f.close()

line.split(',')[0] does therby give us only the first field of every line which we compare with the user's input

Sign up to request clarification or add additional context in comments.

4 Comments

I have changed the code to what you have given, I understand the problem but the line is not being deleted in the csv file. Not so sure what I am doing wrong. Thank you for your help.
When calling the split fucntion, I forgot to specify that we need to split at ,. Edited
Thank you! Works perfectly, I suppose the line.split function and the [0] is for letting the program know what value to look for in the line?
split creates a list of elements in the line and then [0] selects the first element

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.