Q: Deleting a row from a csv(Python) So, I'm trying to make a function to delete a row from my csv depending on the Name given by the parameter.
Original File:
Janet,5,cats
Wilson,67,dogs
Karen,8,mice
John,12,birds

My Code:
csv_remove("Karen")

Intended File:
Janet,5,cats
Wilson,67,dogs
John,12,birds

However, when I execute my code, I get weird newlines everywhere.
Janet,5,cats

Wilson,67,dogs

John,12,birds



Here is the full code:
def csv_remove(name):
    element_list = []
    with open(csv_path, 'r') as j:
        csv_file = csv.reader(j)
        for row in csv_file:
            element_list.append(row)
            if row[0] == name:
                element_list.remove(row)
    with open(csv_path, 'w') as j:
        csv_file = csv.writer(j)
        csv_file.writerows(element_list)

csv_remove("Karen")


A: Read the documentation. When opening a file for writing using the csv-module you need to
supply newline="":
Source:  https://docs.python.org/3/library/csv.html#csv.writer

csv.writer(csvfile, dialect='excel', **fmtparams)
Return a writer object responsible for converting the user's data into delimited strings on the given file-like object. csvfile can be any object with a write() method. If csvfile is a file object, it should be opened with newline=''

The csv-module handles newlines itself. If you do not specify newline='' for the opened file, it will muck up the line endings and you end up with empty lines in it.
