Python CSV File Processing: A Comprehensive Guide from Reading to Conditional Writing

Nov 21, 2025 · Programming · 12 views · 7.8

Keywords: Python | CSV Processing | File I/O | Data Filtering | Programming Errors

Abstract: This article provides an in-depth exploration of reading and conditionally writing CSV files in Python, analyzing common errors and presenting solutions based on high-scoring Stack Overflow answers. It details proper usage of the csv module, including file opening modes, data filtering logic, and write optimizations, while supplementing with NumPy alternatives and output redirection techniques. Through complete code examples and step-by-step explanations, developers can master essential skills for efficient CSV data handling.

Fundamentals of CSV File Processing

Handling CSV (Comma-Separated Values) files in Python is a common task in data analysis and everyday programming. The csv module provides specialized functions for reading and writing this format. Let's first understand the basic reading operations.

Analysis of Original Code Issues

The user's provided code has several critical issues: first, the file opening mode uses 'rb' (binary read), which can cause encoding problems when handling text files in Python 3. More importantly, the writing logic contains a serious flaw—repeatedly opening the file inside the loop, which overwrites previous content in each iteration.

Original code example:

import csv
import collections

with open('test.csv', 'rb') as f:
    data = list(csv.reader(f))

counter = collections.defaultdict(int)
for row in data:
    counter[row[1]] += 1

for row in data:
    if counter[row[1]] >= 4:
        writer = csv.writer(open("test1.csv", "wb"))
        writer.writerows(row)

Correct Implementation Solution

Based on the best answer correction, we need to ensure the file is opened only once when needed and use the correct mode. Here is the improved code:

import csv
import collections

# Read CSV file
with open('thefile.csv', 'r', newline='') as f:
    data = list(csv.reader(f))

# Count occurrences of values in the second column
counter = collections.defaultdict(int)
for row in data:
    if len(row) > 1:  # Ensure the row has enough columns
        counter[row[1]] += 1

# Conditional writing: only keep rows with occurrence count ≥ 4
with open('output.csv', 'w', newline='') as f:
    writer = csv.writer(f)
    for row in data:
        if len(row) > 1 and counter[row[1]] >= 4:
            writer.writerow(row)

Key Improvement Points Explained

File Opening Mode: Change 'rb' and 'wb' to 'r' and 'w', as CSV is a text format. Adding the newline='' parameter avoids newline issues across different operating systems.

Writing Logic Optimization: Move the writer creation outside the loop to avoid repeatedly opening the file. Use writerow() instead of writerows(), as the latter expects a list of rows, and a single row causes errors.

Boundary Checking: Add len(row) > 1 check to prevent index out-of-range errors.

Alternative Approach: Using NumPy

While the csv module is the standard choice, NumPy offers more concise array operations. As shown in the supplementary answer:

import numpy as np

names = ['Player Name', 'Foo', 'Bar']
scores = ['Score', 250, 500]

np.savetxt('scores.csv', [p for p in zip(names, scores)], delimiter=',', fmt='%s')

This method is suitable for numerical data but lacks the fine-grained control of the csv module.

Output Redirection Techniques

The reference article mentions saving print output to a file. Although not directly related, this idea can be extended: use redirection in the command line (e.g., python script.py > output.csv) or print(..., file=f) in code. Note that print() returns None and cannot be directly used as an argument for writerow().

Summary and Best Practices

When processing CSV files, always use context managers (with statements) to ensure proper file closure. For conditional filtering, complete data processing in memory first, then write once. Choose the csv module for general text processing or NumPy for numerical-intensive tasks. By understanding these core concepts, common pitfalls can be avoided, leading to robust CSV processing code.

Copyright Notice: All rights in this article are reserved by the operators of DevGex. Reasonable sharing and citation are welcome; any reproduction, excerpting, or re-publication without prior permission is prohibited.