CSV fields can contain delimiters inside quoted values.
Use a CSV parser rather than splitting every line at commas. Parsing produces strings by default; later steps must validate their meaning.
The bigger picture
Data preparation changes the representation used by later calculations. Every conversion, dropped row or missing-value decision can change the result. Preserve raw inputs so transformations can be inspected and repeated.
- Preserve raw data
- Parse
- Validate records
- Report changes
Concepts to keep close
- Schema
- The expected fields, value types and constraints for a record.
- Missing value
- An absent observation whose meaning must be distinguished from zero or an empty label.
- Audit trail
- A record of transformations, rejected rows and reasons for changes.
Reasoning through the problem
Read the header and expected fields.
Use a parser that handles quoted commas.
Inspect parsed values before conversion.
Use the idea carefully
Inspect a few raw rows and define a schema before writing transformations. Parse structured formats with appropriate tools, validate each record and report rejected inputs. Keep missing and zero distinct. Test the cleaning function on deliberately malformed examples and confirm that summary counts account for every input row.