How to Read CSV Files in Python: 3 Methods to Skip Unwanted Rows
CSV (Comma-Separated Values) files are one of the simplest formats for storing structured data, but they often come with extra challenges — such as inconsistent delimiters, comment lines, or unwanted blank rows. In real-world projects, directly loading CSVs without cleaning them can cause parsing errors or lead to inaccurate results.
This article explores three practical ways to read CSV files in Python while filtering out unwanted rows:
- Python’s built-in
csvmodule — lightweight and dependency-free. - Pandas — powerful for data analysis and large datasets.
- Spire.XLS — useful when working with Excel features and exporting clean CSV data into Excel format.
By the end, you’ll understand not only how to read CSVs safely but also how to choose the right method depending on your use case.
On this page:
- Sample Input File (sample.csv)
- Read CSV Using Python
csvModule - Read CSV Using Pandas
- Read CSV Using Spire.XLS
- Side-by-Side Comparison
- Final Thoughts
Sample Input File (sample.csv)
# This is a comment
Name;Age;Country
Alice;30;USA
Bob;25;UK
Charlie;35;CanadaRead CSV Using Python csv Module
Code Example
import csv
input_file = "C:/Users/Administrator/Desktop/sample.csv"
clean_data = []
with open(input_file, newline="", encoding="utf-8") as f:
reader = csv.reader(f, delimiter=";")
for row in reader:
if not row or row[0].startswith("#"):
continue
clean_data.append(row)
for row in clean_data:
print("\t".join(row)) Output
Name Age Country
Alice 30 USA
Bob 25 UK
Charlie 35 CanadaHow This Works
1. csv.reader reads the file line by line and splits each row into a list of — strings, based on the delimiter (; here).
2. Skipping logic:
not row→ catches fully empty rows.row[0].startswith(“#”)→ ignores comment lines.
3. The cleaned data is saved and printed.
Tips & Variations
1. If you want to skip the header row, you can extend the condition:
if not row or row[0].startswith("#") or row[0] == ("Name"):
continueReplace “Name” with the actual first column header of your CSV file.
2. If you want the data as dictionaries (with column names as keys), you can use csv.DictReader instead:
import csv
input_file = "C:/Users/Administrator/Desktop/sample.csv"
clean_data = []
with open(input_file, newline="", encoding="utf-8") as f:
reader = csv.DictReader(f, delimiter=";")
for row in reader:
if not any(row.values()):
continue
clean_data.append(row)Read CSV Using Pandas
Code Example
import pandas as pd
input_file = "C:/Users/Administrator/Desktop/sample.csv"
df = pd.read_csv(input_file, sep=";", skiprows=1)
df = df.dropna(how="all")
print(df.to_string(index=False))Output
Name Age Country
Alice 30 USA
Bob 25 UK
Charlie 35 CanadaHow This Works
Get Alexander Stock’s stories in your inbox
Join Medium for free to get updates from this writer.
1. pd.read_csv(file, sep=”;”, skiprows=1)
- Reads the CSV into a
DataFrame(like a table). sep=”;”→ tells Pandas your file uses semicolons instead of commas.skiprows=1→ skips the first line (useful if your file has comments or metadata before the header).
2. dropna(how=”all”)
- Removes rows where all columns are empty.
3. print(df.to_string(index=False))
- Prints the DataFrame as a formatted table.
index=Falsehides the automatic row numbers (0, 1, 2) that Pandas would normally display.
Tips & Variations
- If your file has 2 comment lines instead of 1:
df = pd.read_csv(input_file, sep=";", skiprows=2)2. If you want to drop rows with any empty values (not just fully empty rows):
df = df.dropna(how="any")Read CSV Using Spire.XLS
Code Example
from spire.xls import *
from spire.xls.common import *
input_file = "C:/Users/Administrator/Desktop/sample.csv"
# Load the CSV file into the workbook
workbook = Workbook()
workbook.LoadFromFile(input_file, ";", 1, 1)
worksheet = workbook.Worksheets[0]
# Remove blank/comment rows
row = 1
while row <= worksheet.LastRow:
first_cell_value = worksheet.Range[row, 1].Value.strip()
if first_cell_value == "" or first_cell_value.startswith("#"):
worksheet.DeleteRow(row)
else:
row += 1
# (Optional) Print the cleaned data as a table
row_count = worksheet.LastRow
col_count = worksheet.LastColumn
for r in range(1, row_count + 1):
row_values = []
for c in range(1, col_count + 1):
row_values.append(worksheet.Range[r, c].Value.strip())
print("\t".join(row_values))
# Save the cleaned Excel file
workbook.SaveToFile("Cleaned.xlsx", ExcelVersion.Version2016)
workbook.Dispose()Output
Name Age Country
Alice 30 USA
Bob 25 UK
Charlie 35 CanadaHow This Works
1. Loading CSV into Excel worksheet
workbook.LoadFromFile(input_file, “;”, 1, 1)reads the CSV file using ; as the delimiter, and places the content starting at cell A1 (row 1, column 1) of the worksheet.
Important: This function does not skip comment lines, headers, or empty rows — you must handle that manually.
2. Filtering out unwanted rows:
first_cell_value == “”→ skips blank rows.first_cell_value.startswith(“#”)→ skips comment lines.worksheet.DeleteRow(row)removes the unwanted row; the loop does not increment row in this case.
3. The cleaned data is printed as a table and saved to an Excel file.
Tips & Variations
1. To save the cleaned data in a Python list instead of printing or saving to Excel:
clean_data = []
for r in range(1, row_count + 1):
row_values = [worksheet.Range[r, c].Value.strip() for c in range(1, col_count + 1)]
clean_data.append(row_values)2 . To skip multiple comment types (e.g., //), modify the condition:
if first_cell_value == "" or first_cell_value.startswith(("#", "//")):
worksheet.DeleteRow(row)Side-by-Side Comparison
Final Thoughts
Handling CSV files in Python doesn’t have to be complicated. The method you choose depends on your project goals:
- Use the csv module for lightweight parsing when you just need quick access to rows and columns.
- Use Pandas if you need built-in functions for data cleaning, transformation, and analysis.
- Use Spire.XLS when you need seamless CSV-to-Excel workflows, formatting options, or integration with spreadsheet applications.
Each approach has its strengths, and knowing when to apply them can save time and reduce errors. With these three methods, you can confidently read CSV files, skip unnecessary rows, and prepare your data for further processing or analysis.








