0% found this document useful (0 votes)
7 views13 pages

Python CSV Handling: Complete Guide

This tutorial provides comprehensive guidance on handling CSV files in Python, covering both the built-in csv module and the pandas library. It includes detailed explanations of methods, practical examples, advanced patterns, error handling, and best practices for reading, writing, and processing CSV data. Additionally, it features creative mini-projects and exercises to reinforce learning.

Uploaded by

imranriyaz377
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views13 pages

Python CSV Handling: Complete Guide

This tutorial provides comprehensive guidance on handling CSV files in Python, covering both the built-in csv module and the pandas library. It includes detailed explanations of methods, practical examples, advanced patterns, error handling, and best practices for reading, writing, and processing CSV data. Additionally, it features creative mini-projects and exercises to reinforce learning.

Uploaded by

imranriyaz377
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Comprehensive CSV File Handling in Python —

Expanded Tutorial
Detailed explanations, 50+ methods/parameters, and practical examples — ready to paste into a
document or print as a PDF.

Author: ChatGPT (educational tutorial) Date: October 3, 2025


Table of Contents
1. Quick Overview
2. Basic csv module (reader/writer)
3. DictReader & DictWriter
4. Dialects, Sniffer & quoting rules
5. 60+ Methods, Parameters & Useful Symbols (explanations)
6. Practical Examples — reading, writing, transforming (code)
7. Advanced patterns: chunking, merging, compressed files
8. Using pandas for CSV: parameters + DataFrame methods
9. Error handling & best practices
10. Creative mini-projects and exercises
11. Appendix: cheat-sheet & references
1. Quick Overview
CSV (Comma-Separated Values) is a simple text format to store tabular data. Python offers the built-in csv
module for lightweight tasks and pandas for higher-level data analysis. This tutorial covers both: how to
read, write, detect formats, handle edge cases, and perform advanced operations.

2. Basic csv module (reader / writer)


The csv module is part of Python's standard library. It reads rows as lists ([Link]) and writes lists to
rows ([Link]). It always reads strings; convert types as needed.
# Basic read with [Link]
import csv

def read_row_by_row(path):
with open(path, 'r', newline='', encoding='utf-8') as f:
reader = [Link](f, delimiter=',', quotechar='"')
header = next(reader, None) # header is a list
print('Header:', header)
for row in reader:
# each row is a list of strings
print(row)

# Basic write with [Link]


def write_rows(path, rows):
with open(path, 'w', newline='', encoding='utf-8') as f:
writer = [Link](f, delimiter=',', quotechar='"', quoting=csv.QUOTE_MINIMAL)
[Link](['Name', 'Age', 'City'])
[Link](rows)

# Example usage
rows = [['Alice', '30', 'New York'], ['Bob', '24', 'London']]
write_rows('demo_basic.csv', rows)
read_row_by_row('demo_basic.csv')

Notes: Always use newline='' when dealing with csv module to avoid extra blank lines on Windows.
3. DictReader & DictWriter
DictReader turns rows into dicts using header names as keys. DictWriter writes dicts into CSV rows. They
are convenient when column order is known by name.
import csv

def read_as_dict(path):
with open(path, 'r', newline='', encoding='utf-8') as f:
reader = [Link](f)
print('Fields:', [Link])
for row in reader:
print(row) # row is an OrderedDict/dict-like object

def write_from_dict(path, fieldnames, dict_rows):


with open(path, 'w', newline='', encoding='utf-8') as f:
writer = [Link](f, fieldnames=fieldnames)
[Link]()
[Link](dict_rows)

rows = [{'Name':'Grace', 'Age': '32', 'City':'Lahore'}, {'Name':'Heidi','Age':'45','City':'Karachi'}]


write_from_dict('demo_dict.csv', ['Name','Age','City'], rows)
read_as_dict('demo_dict.csv')
4. Dialects, Sniffer & quoting rules
A Dialect groups formatting rules (delimiter, quotechar, etc.). Sniffer can guess dialects from sample text
and detect whether a header exists. Quoting controls how fields with special characters are handled.
import csv

# Using Sniffer to guess dialect


sample = 'Name;Age;City\nAlice;30;New York\n'
sniffer = [Link]()
dialect = [Link](sample) # guesses delimiter and quoting
has_header = sniffer.has_header(sample)
print('Guessed delimiter:', [Link], 'Has header?', has_header)

# Registering a dialect
csv.register_dialect('pipes', delimiter='|', quotechar='"', quoting=csv.QUOTE_MINIMAL)
# Later use: [Link](f, dialect='pipes')

Quoting constants: csv.QUOTE_MINIMAL, csv.QUOTE_ALL, csv.QUOTE_NONNUMERIC,


csv.QUOTE_NONE
5. 60+ Methods, Parameters & Useful Symbols (explanations)
[Link]: Create an object to iterate over lines in CSV as lists.
[Link]: Create an object to write rows (lists) to CSV.
[Link]: Read rows into dicts keyed by header.
[Link]: Write dicts to CSV rows.
csv.field_size_limit(): Get/set maximum field size allowed.
csv.register_dialect(): Register a custom dialect name with parameters.
csv.unregister_dialect(): Remove a dialect registration.
csv.list_dialects(): List registered dialect names.
csv.get_dialect(): Retrieve a Dialect instance by name.
[Link](): Sniffer class used to deduce dialect and header presence.
[Link](): Guess the dialect from a sample string.
Sniffer.has_header(): Guess whether a sample contains a header row.
reader.line_num: Property: number of lines read so far.
[Link]: List of field names used by the DictReader.
DictReader.line_num: Position (line number) within the file.
[Link](): Write header row using defined fieldnames.
[Link](): Write a single row (list) to CSV.
[Link](): Write multiple rows from a sequence of lists.
[Link](): Write a single dict row.
[Link](): Write multiple dict rows.
[Link]: Character separating fields (',' ';' '\t' '|' ...).
[Link]: Character used to quote fields containing special chars.
[Link]: Quoting style constant (QUOTE_MINIMAL, QUOTE_ALL, ...).
[Link]: Character used to escape delimiters when QUOTE_NONE is used.
[Link]: Whether to double quotequotechar inside a field.
[Link]: Whether to skip spaces after delimiter.
[Link]: String used to terminate lines when writing.
csv.QUOTE_MINIMAL: Quote only fields when required.
csv.QUOTE_ALL: Quote all fields.
csv.QUOTE_NONNUMERIC: Quote all non-numeric fields and convert read fields to float.
csv.QUOTE_NONE: Never quote fields (requires escapechar).
[Link]: Base exception class for csv module errors.
open(..., newline=''): Important pattern when reading/writing CSV to avoid blank lines.
encoding: Specify file encoding, e.g., 'utf-8', 'utf-8-sig', 'latin-1'. Useful for BOM & legacy files.
delimiter parameter: Override default delimiter when using reader/writer.
quotechar parameter: Override default quote character.
quoting parameter: Choose quoting behavior for writer/reader.
escapechar parameter: Char to escape delimiters when QUOTE_NONE is used.
skipinitialspace parameter: Trim space after delimiters.
pandas.read_csv: Powerful CSV loader with many options (see section 8).
DataFrame.to_csv: Write DataFrame to CSV file, supports many options.
chunksize: Read CSV in chunks (number of rows) to handle large files.
iterator=True: Return TextFileReader which can be iterated or used with get_chunk().
usecols: Select subset of columns when reading with pandas.
dtype: Specify column dtypes to avoid incorrect inference.
parse_dates: Parse columns as datetimes while reading.
infer_datetime_format: Speed up parsing when formats are consistent.
na_values: Additional strings to treat as NA/NaN.
true_values / false_values: Interpret particular strings as booleans.
on_bad_lines: Control behavior when encountering bad rows (skip, warn, raise or custom function).
compression: Read/write compressed CSV (gzip, bz2, zip, xz).
low_memory: Control internal memory optimization while parsing mixed dtypes.
memory_map: Use mmap when reading for potential speed improvements.
[Link](): Return first n rows.
[Link](): Return last n rows.
[Link](): Summary: index, dtypes, non-null counts.
[Link](): Summary statistics for numeric cols.
[Link](): Drop rows/cols with NA.
[Link](): Fill missing values.
DataFrame.drop_duplicates(): Drop duplicate rows.
DataFrame.sort_values(): Sort by columns.
[Link](): Group data and aggregate.
[Link](): Database-style joins of DataFrames.
[Link](): Concatenate DataFrames along an axis.
6. Practical Examples — reading, writing, transforming (detailed code)
6.1 Read with different encodings and converters
# Using converters and dtype in pandas
import pandas as pd

def load_with_converters(path):
# converters: dict mapping column name to a function
converters = {'Age': lambda x: int(x) if x not in ('', 'NA') else None}
df = pd.read_csv(path, dtype={'Name': 'string'}, converters=converters, parse_dates=['JoinDate'], infer_dat
print([Link]())
return df

# Example: reading with utf-8-sig to handle BOM


df = pd.read_csv('demo_unicode.csv', encoding='utf-8-sig')

6.2 Chunked processing: read big CSV in pieces


# Process large CSV by chunks to reduce memory usage
import pandas as pd

def process_in_chunks(path, chunksize=100000):


reader = pd.read_csv(path, chunksize=chunksize, iterator=True)
agg = []
for i, chunk in enumerate(reader):
# Example: compute mean of a column per chunk
chunk_mean = chunk['value'].mean()
print(f'Chunk {i} mean:', chunk_mean)
[Link](chunk_mean)
# Combine results
return agg

# process_in_chunks('big_file.csv')
7. Advanced patterns: merging multiple CSVs, compressed files,
incremental writes
7.1 Merge a directory of CSVs
import pandas as pd
from pathlib import Path

def merge_csvs(folder, pattern='*.csv'):


p = Path(folder)
parts = []
for i, fp in enumerate([Link](pattern)):
df = pd.read_csv(fp)
df['__source_file'] = [Link] # keep track of origin
[Link](df)
big = [Link](parts, ignore_index=True)
big.to_csv('[Link]', index=False)
return big

# merge_csvs('data/')

7.2 Reading compressed CSV (gzip)


import pandas as pd

# pandas can read compressed files transparently


df = pd.read_csv('[Link]', compression='gzip')
# or with the csv module using [Link]
import gzip, csv
with [Link]('[Link]', 'rt', encoding='utf-8') as f:
reader = [Link](f)
for row in reader:
print(row)
8. Using pandas for CSV: parameters & DataFrame methods
pandas.read_csv is feature-rich. Below are commonly used parameters and code examples. Many of
these provide efficiency or robustness for real-world data.
Common read_csv parameters (short list):
- filepath_or_buffer
- sep
- delimiter
- header
- names
- index_col
- usecols
- dtype
- converters
- true_values
- false_values
- skiprows
- nrows
- na_values
- keep_default_na
- parse_dates
- infer_datetime_format
- date_parser
- dayfirst
- cache_dates
- compression
- thousands
- decimal
- quotechar
- quoting
- escapechar
- comment
- encoding
- encoding_errors
- engine
- squeeze
- memory_map
- float_precision
- on_bad_lines
- warn_bad_lines
- skip_blank_lines
- low_memory
- mangle_dupe_cols
- chunksize
- iterator
Example: robust read_csv with type hints and column selection
import pandas as pd

df = pd.read_csv('[Link]',
usecols=['date','product_id','price','quantity'],
dtype={'product_id':'string','price':'float64','quantity':'Int64'},
parse_dates=['date'],
na_values=['','NA','null'],
on_bad_lines='skip',
encoding='utf-8')
9. Error handling & best practices
- Use context managers (with open(...)) so files close automatically.
- Specify encoding (utf-8 or utf-8-sig) to avoid BOM and UnicodeDecodeError.
- Use newline='' with csv module to avoid blank lines.
- Prefer pandas for heavy lifting; csv module for simple, lightweight tasks.
- Validate headers and data types early (use dtype, converters).
- Use chunksize and iterator when memory is limited.
- Log and handle bad lines using on_bad_lines argument.
- Test with representative samples before processing entire dataset.
# Example: robust conversion with try/except when using csv module
import csv

def safe_read_and_convert(path):
with open(path, 'r', newline='', encoding='utf-8') as f:
reader = [Link](f)
for i, row in enumerate(reader, start=1):
try:
row['Age'] = int(row['Age']) if row['Age'] else None
except ValueError:
print(f'Warning: row {i} age invalid: {row["Age"]}')
row['Age'] = None
# process row...
10. Creative mini-projects and exercises
CSV Validator CLI: Write a command-line tool that validates CSV structure against a schema (column
names, types, required columns). Use csv or pandas and provide nice error messages.
CSV to JSON incremental converter: Read a huge CSV in chunks and write out compressed JSON lines
(.[Link]) for downstream processing.
CSV Data Profiler: Build a script that profiles columns (type, missing %, unique values, frequent values)
and outputs a summary report as CSV/HTML.
Streaming dedupe: Stream-sort or hash-based deduplication of very large CSVs without loading all data
into memory.
Visualizer: Read CSV into pandas and produce basic plots (histograms, boxplots) and export small PNGs
(use matplotlib).

Exercises:
1. Write a script that reads a CSV, normalizes whitespace, and writes a cleaned CSV.
2. Take two CSVs and perform an inner join on a key field using [Link].
3. Create a chunked aggregator that computes sums per day for a very large transactions CSV.
11. Appendix: Quick Cheat-sheet & references
Open files (csv): with open('[Link]','r',newline='',encoding='utf-8') as f: reader = [Link](f)
Write files (csv): with open('[Link]','w',newline='',encoding='utf-8') as f: writer = [Link](f);
[Link](rows)
Read pandas: df = pd.read_csv('[Link]', usecols=['a','b'], dtype={'a':'Int64'})
Chunk read: for chunk in pd.read_csv('[Link]', chunksize=100000): ...
Merge files: [Link]([...], ignore_index=True)

End of tutorial. Practice, explore real datasets, and extend these patterns into larger projects.

You might also like