How to Convert CSV Column Types While Reading in Python

Read a CSV file and automatically convert column values to int, float, str, or bool based on type suffixes in the header names.

Easy Python 3.9+ Aug 9, 2026 Files & data 12 views 0 copies

Python code

40 lines
Python 3.9+
import csv
from pathlib import Path
from typing import Any

def read_csv_with_types(filepath: str) -> list[dict[str, Any]]:
    """Read CSV and convert column types based on header suffixes."""
    converters = {
        "int": int,
        "float": float,
        "str": str,
        "bool": lambda v: v.strip().lower() == "true",
    }
    
    rows = []
    with Path(filepath).open(newline="") as f:
        reader = csv.DictReader(f)
        for raw_row in reader:
            typed_row = {}
            for header, value in raw_row.items():
                # If header contains a type suffix like "age:int", use it
                if ":" in header:
                    column_name, type_name = header.split(":", 1)
                    typed_row[column_name] = converters[type_name](value)
                else:
                    typed_row[header] = value
            rows.append(typed_row)
    return rows

if __name__ == "__main__":
    import tempfile
    sample = "name:str,age:int,score:float,active:bool\nAlice,30,95.5,true\nBob,25,88.0,false\n"
    with tempfile.NamedTemporaryFile(mode="w", suffix=".csv", delete=False) as f:
        f.write(sample)
        temp_path = f.name
    
    result = read_csv_with_types(temp_path)
    for row in result:
        print(f"{row['name']}: age={row['age']} ({type(row['age']).__name__}), "
              f"score={row['score']} ({type(row['score']).__name__}), "
              f"active={row['active']} ({type(row['active']).__name__})")

Output

stdout
Alice: age=30 (<class 'int'>), score=95.5 (<class 'float'>), active=True (<class 'bool'>)
Bob: age=25 (<class 'int'>), score=88.0 (<class 'float'>), active=False (<class 'bool'>)

How it works

The csv.DictReader reads each row as a dictionary with header names as keys and string values. The custom type suffix in the header (e.g., age:int) tells the code which converter to apply. A mapping of type names to callables (int, float, str, and a lambda for booleans) is used to transform each value. Splitting the header on the colon separates the real column name from the type hint, so the output dictionary has clean keys with properly typed values.

Common mistakes

  • Using `json.load` instead of `csv.DictReader` for CSV data
  • Not handling whitespace in header names like `age: int`
  • Assuming all rows have the same headers or missing values
  • Forgetting to strip values before converting booleans

Variations

  1. Use `pandas.read_csv` with `dtype` parameter to specify types per column
  2. Use `csv.reader` with manual column mapping instead of `DictReader`

Real-world use cases

  • Loading CSV exports from databases or APIs where types are lost during export.
  • Preparing CSV data for machine learning models that require numeric and boolean features.
  • Cleaning and typing customer or product data before inserting into a SQL database.

Sponsored

Run this sample

Open the browser IDE to tweak the example and see results without installing anything.

Open editor

More from Files & data

Related tutorials and quizzes for this topic.