Recent chats
ASKGEEZ Q&A GUIDETechnology & devices

How can I count CSV records while ignoring blank rows?

Quick answer

Use Python’s csv.reader and count each parsed row that is nonempty: if row: count += 1. Open the file with newline=''. This ignores empty parsed rows but keeps rows made of empty fields, such as the two-field row represented by ,. Counting physical lines can be wrong because a quoted CSV field may contain a newline and still belong to one record. [S1]

LET’S WORK THROUGH IT

The question, the details, the next step.

Question

What is the simplest way to count CSV records while ignoring blank rows? I want a count of parsed records, not just the number of lines in the file.

Geez

Open the file with newline='', pass it to csv.reader, and count each nonempty parsed row. For example:

import csv

count = 0
with open("data.csv", newline="") as f:
    for row in csv.reader(f):
        if row:
            count += 1

print(count)

Here, if row skips a row only when the reader returns an empty list. The standard reader skips a blank physical line; a line containing a delimiter can instead parse as fields containing empty strings. CSV fields can also contain newlines, so counting physical lines does not necessarily count records. [S1]

This counts every nonempty parsed record, including a header if the file has one. If you want data records only, decide separately whether to exclude the header.

Follow-up question

What exactly is the difference between a blank row and a row that contains empty fields?

Geez

A blank physical line is skipped by the standard csv.reader. By contrast, a line containing a comma has a delimiter, so it can produce a parsed row with two empty-string fields. Because that row is still a list with entries, if row keeps it. [S1]

For a simplified illustration, imagine these lines in a CSV file:

name,city

,
Mina,

The blank line contributes no parsed row under the standard reader. The line containing just , represents two empty fields and is retained by the count. Mina, also represents a row and is retained, even though its second field is empty. This approach does not mean “skip any record with missing values”; it means “skip only an empty parsed row.”

Follow-up question

Why not use sum(1 for line in f if line.strip())? It looks shorter.

Geez

That expression counts nonblank physical lines, not parsed CSV records. A quoted field can contain a newline, so a single CSV record may span multiple physical lines. Counting lines could therefore give a different result from iterating over parsed rows. [S1]

It can also blur distinctions you may care about: a line containing commas is not whitespace-only, even if all its fields are empty. To count according to CSV structure, let the reader parse the file first, then apply the empty-row condition to each parsed result.

The example assumes the file is intended to use the dialect selected by csv.reader’s defaults. If the file uses a different delimiter or quoting convention, configure the reader for that dialect; parsing choices affect what counts as a row. [S1]

Follow-up question

Can you show a small worked example, including a record with a quoted newline? And what should I change if there is a header?

Geez

Suppose the file contains a header, a blank line, a two-empty-field row, and a record whose quoted field spans two physical lines:

name,note

,
Mina,"first line
second line"

Using the standard reader and counting nonempty rows gives three parsed records: the header, the row represented by ,, and Mina’s record. The blank physical line is skipped. Mina’s entry is one parsed record even though its quoted field occupies two physical lines. [S1]

To count data records but not the header, skip the first nonempty parsed row:

import csv

count = 0
header_seen = False
with open("data.csv", newline="") as f:
    for row in csv.reader(f):
        if not row:
            continue
        if not header_seen:
            header_seen = True
            continue
        count += 1

print(count)

This treats the first nonempty parsed row as the header; it does not verify that the row contains particular column names. The example explains standard-reader behavior, not every possible CSV dialect or malformed-file case. [S1]

A practical checklist

  • Open the file with newline='' and use csv.reader.

  • Count parsed rows rather than physical lines.

  • Use if row to skip empty parsed rows while keeping rows containing empty fields.

  • Decide whether the header belongs in your count; if not, skip the first nonempty parsed row.

  • Set the reader’s dialect appropriately if the file does not use the defaults.

A few more useful answers

Does `if row` remove a row containing only empty fields?

No. A parsed row such as ['', ''] is a nonempty list, so if row keeps it. That is different from an empty parsed row, represented by an empty list.

Should I use `newline=''` when opening the file?

Yes. The Python documentation recommends it so the CSV module handles newlines correctly, including newlines inside quoted fields. [S1]

Does this count the header?

The basic count includes every nonempty parsed row, including a header. Skip the first nonempty row if your goal is to count data records only.

Sources

References used for this guide.

  1. [S1] Python 3.14 csv module: reader behavior, dialects, and line handling
    docs.python.org · Checked

YOUR QUESTION. YOUR DETAILS.

Let’s work through your situation.

Get a tailored answer or help with your next task. Starts at $1, with a quote before you pay.

Ask your own question
← Back to Geez at Work