ReviseAlgo Logo

File Handling

CSV Files

Comprehensive CSV handling — reader/writer, DictReader/DictWriter, custom dialects, and processing large CSV files efficiently.

Interview: CSV processing is common in data engineering and backend interviews — know both basic and DictReader approaches.

Last Updated: June 12, 2026 9 min read

CSV (Comma-Separated Values) is the simplest tabular data format. Python's csv module provides robust reading and writing with support for different delimiters, quoting styles, and dictionary-based access for more readable code.

Reader and Writer

  • csv.reader(file) — returns each row as a list of strings
  • csv.writer(file) — writes rows using writerow() and writerows()
  • Always open CSV files with newline='' to prevent extra blank lines on Windows
  • All values are read as strings — you must manually convert to int/float

DictReader and DictWriter

  • csv.DictReader(file) — returns each row as a dict using header row as keys
  • csv.DictWriter(file, fieldnames) — writes dicts, requires writeheader() first
  • More readable than index-based access — row['name'] vs row[0]
  • Extra fields go into a configurable restkey, missing fields use restval

CSV Dialects

Different CSV formats use different delimiters and quoting. Python supports custom dialects:

  • delimiter — column separator (default: comma). TSV uses tab \t
  • quotechar — character for quoting fields containing delimiters (default: ")
  • quoting — when to quote: QUOTE_MINIMAL, QUOTE_ALL, QUOTE_NONNUMERIC, QUOTE_NONE
  • csv.Sniffer — auto-detects the dialect of a CSV file from a sample

Interview Insight

Know the difference between reader and DictReader. Be aware of the newline='' requirement and how to handle CSVs with different delimiters. For large files, process row-by-row instead of loading all into memory.

Use Cases

Data import/export — converting between CSV and database records

Report generation — exporting query results to CSV for Excel

Data pipeline — processing large CSV datasets row-by-row

Configuration — simple tabular configuration files

Log analysis — parsing structured log files in CSV format

Common Mistakes

Not using newline="" when opening CSV files — causes extra blank lines on Windows

Forgetting that csv.reader returns all values as strings — must convert to int/float manually

Loading entire large CSV into memory — process row-by-row with iteration instead

Not calling writeheader() with DictWriter — first data row becomes the header

Hardcoding comma delimiter — use csv module to handle TSV and custom formats