Working with CSV files

CSV files appear everywhere once programs start dealing with real data. They are simple, portable, and easy to inspect, which makes them a practical choice for small datasets that feed into larger systems. In the context of Python programs that generate content or process data for AI-enabled workflows, CSV often acts as a bridge between tools, scripts, and stages of a pipeline.

What CSV files are and when they are useful

A CSV file stores data in rows and columns using plain text. Each row represents a record, and each column represents a field within that record. Values are separated by commas, which keeps the format easy to read and easy to generate.

CSV works well when data is tabular and relatively simple. It is commonly used for lists of planets, moons, measurements, or other structured records that do not need nested structure.

Reading tabular data from a CSV file

Python’s standard library includes the csv module, which knows how to read CSV files safely and consistently. When reading, each row is presented as a sequence of values that corresponds to the columns in the file.

A typical pattern is to open the file and iterate over its rows.

import csv

with open("planets.csv", newline="") as file:
    reader = csv.reader(file)
    for row in reader:
        print(row)

This gives us each row as a list of strings, ready to be inspected or transformed.

Accessing rows and columns of CSV data

Once a row is read, its values can be accessed by position. The first value is at index 0, the next at index 1, and so on.

This positional access makes it easy to pull out specific columns, such as a planet’s name or distance from the sun.

name = row[0]
distance = row[1]

At this stage, CSV data is still text. Converting values to numbers or other types is a separate step handled explicitly in code.

Writing rows of data to a CSV file

CSV files are just as easy to write as they are to read. We open a file for writing, create a writer, and then supply rows as sequences of values.

Each call to the writer produces one row in the output file.

import csv

rows = [
    ["Planet", "Moons"],
    ["Mars", 2],
    ["Jupiter", 79],
]

with open("output.csv", "w", newline="") as file:
    writer = csv.writer(file)
    writer.writerows(rows)

The result is a standard CSV file that can be opened in a spreadsheet or read by another program.

Using CSV files for simple datasets

CSV files are a good fit for small, stable datasets that describe things rather than behavior. They work well as inputs to scripts that generate HTML pages, reports, or intermediate artifacts.

In many Python programs, CSV sits alongside JSON as a lightweight way to move structured data into and out of a system without adding complexity.

Conclusion

At this point, CSV files should feel familiar rather than mysterious. We can recognize when they are useful, read rows and columns from them, and write tabular data back out. That orientation is enough to start using CSV files confidently as part of real Python programs.