Delete blank rows from CSV?

Posted on

Question :

Delete blank rows from CSV?

I have a large csv file in which some rows are entirely blank. How do I use Python to delete all blank rows from the csv?

After all your suggestions, this is what I have so far

import csv

# open input csv for reading
inputCSV = open(r'C:input.csv', 'rb')

# create output csv for writing
outputCSV = open(r'C:OUTPUT.csv', 'wb')

# prepare output csv for appending
appendCSV = open(r'C:OUTPUT.csv', 'ab')

# create reader object
cr = csv.reader(inputCSV, dialect = 'excel')

# create writer object
cw = csv.writer(outputCSV, dialect = 'excel')

# create writer object for append
ca = csv.writer(appendCSV, dialect = 'excel')

# add pre-defined fields

# delete existing field names in input CSV
# ???????????????????????????

# loop through input csv, check for blanks, and write all changes to append csv
for row in cr:
    if row or any(row) or any(field.strip() for field in row):

# close files

Is this ok or is there a better way to do this?

Asked By: debugged


Answer #1:

Use the csv module:

import csv

with open(in_fnam) as in_file:
    with open(out_fnam, 'w') as out_file:
        writer = csv.writer(out_file)
        for row in csv.reader(in_file):
            if row:

If you also need to remove rows where all of the fields are empty, change the if row: line to:

if any(row):

And if you also want to treat fields that consist of only whitespace as empty you can replace it with:

if any(field.strip() for field in row):

Note that in Python 2.x and earlier, the csv module expected binary files,
and so you’d need to open your files with e 'b' flag. In 3.x, doing this will
result in an error.

Answered By: Laurence Gonsalves

Answer #2:

Surprised that nobody here mentioned pandas. Here is a possible solution.

import pandas as pd
df = pd.read_csv('input.csv')
df.to_csv('output.csv', index=False)
Answered By: Sagun Shrestha

Answer #3:

Delete empty row from .csv file using python

    import csv

 with open('demo004.csv') as input, open('demo005.csv', 'w', newline='') as output:
     writer = csv.writer(output)
     for row in csv.reader(input):
         if any(field.strip() for field in row):


Answer #4:

You have to open a second file, write all non blank lines to it, delete the original file and rename the second file to the original name.

EDIT: a real blank line will be like ‘n’:

for line in f1.readlines():
    if line.strip() == '':

a line with all blank fields would look like ‘,,,,,n’. If you consider this a blank line:

for line in f1.readlines():
    if ''.join(line.split(',')).strip() == '':

openning, closing, deleting and renaming the files is left as an exercise for you. (hint: import os, help(open), help(os.rename), help(os.unlink))

EDIT2: Laurence Gonsalves brought to my attention that a valid csv file could have blank lines embedded in quoted csv fields, like 1, 'thisnnis tricky',123.45. In this case the csv module will take care of that for you. I’m sorry Laurence, your answer deserved to be accepted. The csv module will also address the concerns about a line like "","",""n.

Answered By: Paulo Scardine

Answer #5:

python code for remove blank line from csv file without create another file.

def ReadWriteconfig_file(file):

    file_object = open(file, 'r')
    lines = csv.reader(file_object, delimiter=',', quotechar='"')
    flag = 0
    for line in lines:
        if line == []:
            flag =1
    if flag ==1: #if blank line is present in file
        file_object = open(file, 'w')
        for line in data:
            str1 = ','.join(line)
except Exception,e:
    print e
Answered By: vaibhav

Answer #6:

I need to do this but not have a blank row written at the end of the CSV file like this code unfortunately does (which is also what Excel does if you Save-> .csv). My (even simpler) code using the CSV module does this too:

import csv

input = open("M51_csv_proc.csv", 'rb')
output = open("dumpFile.csv", 'wb')
writer = csv.writer(output)
for row in csv.reader(input):

M51_csv_proc.csv has exactly 125 rows; the program always outputs 126 rows, the last one being blank.

I’ve been through all these threads any nothing seems to change this behaviour.

Answered By: Gordon Dennis

Answer #7:

Doing it with pandas is very simple. Open your csv file with pandas:

import pandas as pd
df = pd.read_csv("example.csv")
#checking the number of empty rows in th csv file
print (df.isnull().sum())
#Droping the empty rows
modifiedDF = df.dropna()
#Saving it to the csv file 
Answered By: Hamza Tayyab

Answer #8:

Here is a solution using pandas that removes blank rows.

 import pandas as pd
 df = pd.read_csv('input.csv')
 df.dropna(axis=0, how='all',inplace=True)
 df.to_csv('output.csv', index=False)
Answered By: Aizayousaf

Leave a Reply

Your email address will not be published. Required fields are marked *