On this page

Working with CSV

Enrico Hochkoeppler Profile

By Enrico Hochkoeppler

Web Developer & TutoriaLibre Founder

Why CSV Matters​

CSV (Comma‑Separated Values) is one of the simplest and most universal data formats. It is used for:

Although Python provides a dedicated csv module, this lesson focuses on pure Python techniques—string manipulation, splitting, joining, and careful parsing.

This gives you full control and helps you understand what CSV really is: plain text with a predictable structure.


Understanding CSV Structure​

A CSV file is just text where:

Example:


name,age,city

Alice,30,Osaka

Bob,25,Tokyo

When reading CSV manually, you must handle:


Reading a CSV File Line by Line​

The simplest approach is to read each line and split on commas.


rows = []



with open("people.csv", "r", encoding="utf-8") as f:

    for line in f:

        line = line.strip()

        fields = line.split(",")

        rows.append(fields)

Now:


rows[0]   # ['name', 'age', 'city']

rows[1]   # ['Alice', '30', 'Osaka']

This works for simple CSV files without quoted fields.


Handling Quoted Fields (Basic Approach)​

If a field contains a comma, it must be quoted:


name,comment

Alice,"Hello, world"

A naive .split(",") will break this.

A simple parser can be built using state tracking:


def parse_csv_line(line):

    fields = []

    field = ""

    in_quotes = False



    for char in line:

        if char == '"' and not in_quotes:

            in_quotes = True

        elif char == '"' and in_quotes:

            in_quotes = False

        elif char == "," and not in_quotes:

            fields.append(field)

            field = ""

        else:

            field += char



    fields.append(field)

    return fields

Usage:


with open("comments.csv", "r", encoding="utf-8") as f:

    for line in f:

        print(parse_csv_line(line.strip()))

This handles:


Writing CSV Files (Simple Case)​

To write CSV manually, join fields with commas.


rows = [

    ["name", "age", "city"],

    ["Alice", "30", "Osaka"],

    ["Bob", "25", "Tokyo"]

]



with open("people.csv", "w", encoding="utf-8") as f:

    for row in rows:

        line = ",".join(row)

        f.write(line + "\n")

This produces a valid CSV file for simple data.


Writing CSV with Quoted Fields​

If a field contains a comma, quote it.


def escape_field(value):

    if "," in value or '"' in value:

        value = value.replace('"', '""')

        return f'"{value}"'

    return value

Writing rows:


with open("comments.csv", "w", encoding="utf-8") as f:

    for row in rows:

        escaped = [escape_field(v) for v in row]

        f.write(",".join(escaped) + "\n")

This handles:


Converting CSV to Python Data Structures​

Once parsed, CSV rows become lists of strings.

You can convert them into dictionaries using the header row:


with open("people.csv", "r", encoding="utf-8") as f:

    lines = [line.strip() for line in f]



header = lines[0].split(",")

data = []



for line in lines[1:]:

    fields = line.split(",")

    record = dict(zip(header, fields))

    data.append(record)

Now:


data[0]["name"]   # "Alice"

This is the foundation for building CSV‑based data pipelines.


Best Practices for Pure‑Python CSV Handling​

These practices help you avoid subtle bugs and ensure predictable behavior.


Summary​

In this lesson, you learned how to read and write CSV files using pure Python, without relying on the csv module. You explored splitting lines, parsing quoted fields, writing rows safely, escaping values, and converting CSV data into Python structures.

This foundation prepares you for more advanced persistence techniques and structured data formats.

Found this helpful? Share the knowledge...

... or support this project to keep it alive!

Buy Me a Coffee

Previous

Working with JSON

Next

Serialization with Pickle