Overview
Handles real-world CSV processing tasks reliably: encoding detection, delimiter sniffing, header normalization, row filtering and column selection, merging multiple CSVs, chunked processing for large files, validation against schemas, and export to CSV, JSON, Parquet, or Excel with proper quoting and escaping.
When to Use This Skill
- Ingesting or cleaning data delivered as CSV exports.
- Combining data from multiple CSV sources.
- Converting CSV to other formats for downstream systems.
- The user provides CSV files or describes CSV processing needs.
Prerequisites
- Python with
pandas (recommended for most work) or standard csv module for very simple cases.
- The CSV file(s) or a description of their structure.
- For large files: enough memory or a chunked strategy.
Steps
Inspect the CSV:
- Open with a text editor or
head -c 1000.
- Determine encoding (use
chardet or charset_normalizer).
- Sniff delimiter (
, , ;, \t).
Read safely:
pd.read_csv(..., encoding=..., sep=..., engine='c' or 'python').
dtype=str initially to avoid type inference surprises, then cast.
on_bad_lines='skip' or custom error handler for malformed rows.
Header & column normalization:
- Lowercase, strip, replace spaces with underscores.
- Handle duplicate column names.
- Rename using a mapping if the source headers are bad.
Filtering & selection:
- Boolean indexing for rows.
- Column list or regex for selection.
- Drop or keep based on business rules.
Merging multiple CSVs:
pd.concat with ignore_index=True.
- Or
pd.merge on keys for relational join.
- Handle schema differences between files.
Chunked processing (for >1GB or memory-constrained):
pd.read_csv(..., chunksize=100_000).
- Process each chunk, write to Parquet or append to output.
- Aggregate as you go when possible.
Validation:
- Required columns present.
- No unexpected nulls in key fields.
- Value ranges or allowed sets.
- Row count within expected bounds.
Export:
to_csv(index=False, quoting=csv.QUOTE_MINIMAL).
- Prefer Parquet for large/intermediate data (much faster, smaller, typed).
- Excel with
openpyxl for business users (with formatting).
Output:
- Reusable
process_csv(input_path, output_path) function or notebook cells.
- Handling for common real-world messiness (quotes, newlines in fields, BOM).
Examples
A robust script that reads a messy sales CSV (mixed encodings, bad headers, embedded newlines), cleans it, merges with a second "customers" CSV, validates, and exports to Parquet + a summary Excel report is included.
Edge Cases & Error Handling
- Embedded delimiters or quotes: Use the
csv module's dialect or pandas with proper quoting.
- Huge files on low memory: Always offer the chunked path.
- Date formats: Multiple formats in one column — use
dateutil.parser or pd.to_datetime with format inference.
Verification
- The script runs without crashing on the provided (messy) file.
- Output file has the expected columns, dtypes, and row count.
- Manual spot checks in the output match expectations.
- Re-running on the same input produces bit-identical output (when deterministic).
- Success: The CSV is turned into clean, usable data with a clear audit trail of transformations.
References
1---2name: csv-processor3description: Parses, transforms, validates, and exports CSV files. Use when reading, cleaning, filtering, merging, or converting CSV data.4license: Apache-2.05---67## Overview89Handles real-world CSV processing tasks reliably: encoding detection, delimiter sniffing, header normalization, row filtering and column selection, merging multiple CSVs, chunked processing for large files, validation against schemas, and export to CSV, JSON, Parquet, or Excel with proper quoting and escaping.1011## When to Use This Skill1213- Ingesting or cleaning data delivered as CSV exports.14- Combining data from multiple CSV sources.15- Converting CSV to other formats for downstream systems.16- The user provides CSV files or describes CSV processing needs.1718## Prerequisites1920- Python with `pandas` (recommended for most work) or standard `csv` module for very simple cases.21- The CSV file(s) or a description of their structure.22- For large files: enough memory or a chunked strategy.2324## Steps25261. **Inspect the CSV**:27 - Open with a text editor or `head -c 1000`.28 - Determine encoding (use `chardet` or `charset_normalizer`).29 - Sniff delimiter (`,` , `;`, `\t`).30312. **Read safely**:32 - `pd.read_csv(..., encoding=..., sep=..., engine='c' or 'python')`.33 - `dtype=str` initially to avoid type inference surprises, then cast.34 - `on_bad_lines='skip'` or custom error handler for malformed rows.35363. **Header & column normalization**:37 - Lowercase, strip, replace spaces with underscores.38 - Handle duplicate column names.39 - Rename using a mapping if the source headers are bad.40414. **Filtering & selection**:42 - Boolean indexing for rows.43 - Column list or regex for selection.44 - Drop or keep based on business rules.45465. **Merging multiple CSVs**:47 - `pd.concat` with `ignore_index=True`.48 - Or `pd.merge` on keys for relational join.49 - Handle schema differences between files.50516. **Chunked processing** (for >1GB or memory-constrained):52 - `pd.read_csv(..., chunksize=100_000)`.53 - Process each chunk, write to Parquet or append to output.54 - Aggregate as you go when possible.55567. **Validation**:57 - Required columns present.58 - No unexpected nulls in key fields.59 - Value ranges or allowed sets.60 - Row count within expected bounds.61628. **Export**:63 - `to_csv(index=False, quoting=csv.QUOTE_MINIMAL)`.64 - Prefer Parquet for large/intermediate data (much faster, smaller, typed).65 - Excel with `openpyxl` for business users (with formatting).66679. **Output**:68 - Reusable `process_csv(input_path, output_path)` function or notebook cells.69 - Handling for common real-world messiness (quotes, newlines in fields, BOM).7071## Examples7273A robust script that reads a messy sales CSV (mixed encodings, bad headers, embedded newlines), cleans it, merges with a second "customers" CSV, validates, and exports to Parquet + a summary Excel report is included.7475## Edge Cases & Error Handling7677- **Embedded delimiters or quotes**: Use the `csv` module's dialect or pandas with proper quoting.78- **Huge files on low memory**: Always offer the chunked path.79- **Date formats**: Multiple formats in one column — use `dateutil.parser` or `pd.to_datetime` with `format` inference.8081## Verification82831. The script runs without crashing on the provided (messy) file.842. Output file has the expected columns, dtypes, and row count.853. Manual spot checks in the output match expectations.864. Re-running on the same input produces bit-identical output (when deterministic).875. Success: The CSV is turned into clean, usable data with a clear audit trail of transformations.8889## References9091- [pandas.read_csv](https://pandas.pydata.org/docs/reference/api/pandas.read_csv.html)92- [Python csv module](https://docs.python.org/3/library/csv.html)93- [chardet](https://github.com/chardet/chardet)94- [Parquet vs CSV](https://databricks.com/blog/2018/08/16/how-to-use-parquet-files-in-spark-sql.html)