Computer Programming 1 (Python) • Files & Data I/O

Parsing CSV Files — I can parse a simple CSV file (no quotes) into lists/dictionaries using split at my level.

Learn to read comma-separated data from files and transform it into Python lists and dictionaries.

A Python coding terminal with a program flow and laptop.
Inside this lesson 10 sections 5 questions 5 figures

SECTION 1

Historical Context & Motivation

Long before databases and spreadsheets became everyday tools, people needed a simple way to store and share structured data as plain text. The CSV format — short for Comma-Separated Values — emerged as one of the earliest and most universal ways to represent tabular data. Each line in a CSV file represents a row, and commas separate the individual values in each row. This format is so straightforward that virtually every programming language, spreadsheet application, and database tool can read and write it.

Understanding how to parse CSV files is a foundational skill in programming because real-world data — from science experiments to sports statistics to business records — frequently arrives in this format. Before you can analyze data, you first have to read it into your program and organize it into structures like lists and dictionaries that Python can work with.

1960s • Early Mainframe Era

IBM mainframes begin using comma-delimited text files to exchange data between programs. The simplicity of plain-text formats makes them ideal for limited storage and processing power.

1972 • RFC and Early Standards

Early networking documents describe conventions for transferring tabular data as delimited text. The comma becomes the most popular separator, though tabs and pipes are also used.

1987 • Spreadsheet Software Boom

Lotus 1-2-3 and early versions of Microsoft Excel adopt CSV as an import/export format. This cements CSV as the universal bridge between different software tools.

2005 • RFC 4180 Published

The Internet Engineering Task Force publishes RFC 4180, providing a formal specification for the CSV format. This standardizes rules about headers, line endings, and quoting.

Today • Data Science & Open Data

CSV remains one of the most common formats for datasets on platforms like Kaggle, government open-data portals, and scientific repositories. Python's ability to parse CSV files makes it a go-to language for data analysis.

The central question this lesson addresses is straightforward but powerful: how do you take a flat text file full of commas and line breaks and turn it into organized Python data structures you can loop through, search, filter, and analyze?

SECTION 2

Core Principles & Definitions

Before writing any code, you need to understand the building blocks of CSV parsing. A CSV file is a plain-text file where each line represents one record (row) and fields within that record are separated by a delimiter — in our case, a comma. The first line often contains headers that name each column. Parsing means reading this raw text and converting it into data structures your program can use.

1

Delimiter

The character that separates individual values in a row. In CSV, this is a comma (,). Python's .split(",") method breaks a string at every comma.
2

Header Row

The first line of many CSV files contains column names like "name,age,grade". These headers become the keys when you build dictionaries from the data rows that follow.
3

strip() and Whitespace

Each line read from a file ends with a newline character ( ). Calling .strip() removes leading and trailing whitespace so your data is clean.
4

List of Lists

The simplest structure: a list where each element is itself a list of field values from one row. You can access data by row index and column index, like data[2][1].
5

List of Dictionaries

A more readable structure: each row becomes a dictionary with header names as keys. You access data by name, like row["age"], which is much clearer than using numeric indices.
✦

KEY TAKEAWAY

Think of a CSV file like a table in a notebook. Each line is a row, and the commas act like the vertical lines separating columns. Python's split(",") is like running your finger along a row and tearing it apart at every column line, giving you a list of individual cell values.

SECTION 3

Visual Explanation — From File to Data Structure

CSV File → split(",") → List of Listsstudents.csvname,age,gradeAlice,16,ABob,17,BCarol,15,A.strip().split(",")List of Lists["name","age","grade"]["Alice","16","A"]["Bob","17","B"]["Carol","15","A"]zip withheadersList of Dicts{"name":"Alice", "age":"16", ...}{"name":"Bob", "age":"17", ...}{"name":"Carol", "age":"15", ...}Python Code Flowwithopen("students.csv") asf:headers = f.readline().strip().split(",")data = []forline inf:values = line.strip().split(",")row_dict = dict(zip(headers, values))← strip + split← zip into dict
This diagram shows the three-stage pipeline: raw CSV text is stripped and split into a list of lists, then optionally zipped with headers to produce a list of dictionaries. The code at the bottom shows the complete Python implementation.

The diagram above illustrates the core workflow for parsing a simple CSV file. On the left, the raw file contains plain text with commas separating each value. The .strip() method removes the trailing newline character from each line, and .split(",") breaks the cleaned string at every comma to produce a list of individual field values. When you want more readable access to your data, you pair each value with its corresponding header using Python's zip() function and wrap the result in dict() to create a dictionary for each row.

SECTION 4

How It Works — The Parsing Pipeline Step by Step

Parsing a CSV file is a multi-step process. Each step transforms the data from one form into another, gradually moving from raw text toward organized Python objects. Let's trace each transformation in detail.

Step A — Opening the File

You open the file using Python's built-in open() function, preferably inside a with statement. The with block automatically closes the file when you're done, even if an error occurs. This is considered a best practice because forgetting to close a file can cause data corruption or resource leaks.

Step B — Reading Lines

You can iterate through the file object directly with a for loop, which reads one line at a time. Alternatively, f.readline() reads a single line (useful for grabbing the header), and f.readlines() reads all remaining lines into a list. Each line is a string that ends with a newline character (\n), which you must remove before splitting.

Step C — Stripping Whitespace

Calling line.strip() removes leading and trailing whitespace, including the \n at the end. Without this step, the last field in each row would have an invisible newline character stuck to it, which would cause unexpected behavior when comparing strings or converting to numbers.

Step D — Splitting on the Delimiter

The .split(",") method is the heart of CSV parsing. It takes a string like "Alice,16,A" and returns ["Alice", "16", "A"]. Notice that all values are strings — even numbers. If you need numeric data, you must explicitly convert using int() or float().

Step E — Building Dictionaries with zip()

Python's zip() function pairs elements from two sequences together. When you write zip(["name","age","grade"], ["Alice","16","A"]), it produces pairs: ("name","Alice"), ("age","16"), ("grade","A"). Wrapping these pairs in dict() creates {"name": "Alice", "age": "16", "grade": "A"}. This approach is elegant because it automatically maps each value to its header, regardless of how many columns your file has.

⚠

Important Limitation

This approach assumes a simple CSV with no quoted fields. If a field itself contains a comma — for example, "Smith, John" — then split(",") would incorrectly break it into two fields. For files with quotes, use Python's built-in csv module instead.

SECTION 5

List of Lists vs. List of Dictionaries

When you parse CSV data, you have two main choices for how to store it. Each has trade-offs, and the right choice depends on what you plan to do with the data afterward.

Two Ways to Store Parsed CSV DataLIST OF LISTSdata[0] → ["Alice", "16", "A"]data[1] → ["Bob", "17", "B"]data[2] → ["Carol", "15", "A"]Access Patterndata[0][1] # → "16"↑ row index ↑ column indexYou must remember thatcolumn 1 = age.Pros & Cons✓ Simple to build✓ Less memory✗ Indices are cryptic✗ Easy to mix up columns✗ Harder to maintainLIST OF DICTIONARIES{"name":"Alice","age":"16","grade":"A"}{"name":"Bob","age":"17","grade":"B"}{"name":"Carol","age":"15","grade":"A"}Access Patterndata[0]["age"] # → "16"↑ row index ↑ column nameThe key "age" tells youexactly what you're accessing.Pros & Cons✓ Self-documenting keys✓ Easier to debug✓ Column order doesn't matter✗ Slightly more code✗ Uses a bit more memory
Side-by-side comparison of the two main data structures. A list of lists uses numeric indices, while a list of dictionaries uses descriptive string keys. For most applications, dictionaries are preferred because they make your code more readable.
FeatureList of ListsList of Dictionaries
Access a fieldrow[1]row["age"]
ReadabilityLow — must remember column orderHigh — keys describe the data
Lines of code to buildFewer (no header handling needed)A few more (must read headers first)
Best forQuick scripts, small datasetsLarger programs, shared code

Comparison of the two main CSV storage structures

SECTION 6

Worked Example — Parsing a Student Roster

Let's work through a complete example. Suppose you have a file called roster.csv with the following contents:

📝

roster.csv

name,age,grade,gpa Alice,16,11,3.8 Bob,17,12,3.2 Carol,15,10,3.9 Dave,16,11,2.7

Our goal: read this file into a list of dictionaries, then find the student with the highest GPA.

Parsing roster.csv into Dictionaries

  1. 1

    Step 1 — Open the File

    Use a with statement to open the file for reading. This ensures the file is properly closed when we're done. with open("roster.csv") as f:
  2. 2

    Step 2 — Read and Parse the Header

    Read the first line, strip whitespace, and split on commas to get the column names. headers = f.readline().strip().split(",")
    headers → ["name", "age", "grade", "gpa"]
  3. 3

    Step 3 — Loop Through Remaining Lines

    Initialize an empty list, then loop through each remaining line in the file. For each line, strip whitespace and split on commas to get the values. students = [] for line in f: values = line.strip().split(",")
    First iteration: values → ["Alice", "16", "11", "3.8"]
  4. 4

    Step 4 — Create a Dictionary for Each Row

    Use zip() to pair headers with values, then wrap in dict(). Append each dictionary to the list. row_dict = dict(zip(headers, values)) students.append(row_dict)
    First dictionary → {"name": "Alice", "age": "16", "grade": "11", "gpa": "3.8"}
  5. 5

    Step 5 — Use the Data — Find Highest GPA

    Loop through the list to find the student with the highest GPA. Remember that GPA is stored as a string, so convert it to a float for comparison. best = max(students, key=lambda s: float(s["gpa"])) print(best["name"], best["gpa"])
    Output: Carol 3.9
💡

Pro Tip

You can also skip the max() function and use a simple loop with an if statement to track the highest GPA. Both approaches work — use whichever you're more comfortable with at your current skill level.

SECTION 7

Manual Parsing vs. the csv Module

You might wonder: if Python has a built-in csv module, why learn to parse files manually with split()? Understanding the manual approach builds your foundational skills in string manipulation and file I/O — concepts that transfer to every programming language and every text-based format, not just CSV.

FeatureManual split()csv Module
Handles simple CSV (no quotes)✓ Yes✓ Yes
Handles quoted fields✗ No✓ Yes
Handles commas inside fields✗ No✓ Yes
Requires import statement✗ No (built-in string methods)import csv
Teaches string fundamentals✓ Excellent learning exerciseSomewhat (hides internals)
Production readinessGood for clean, simple dataBetter for messy, real-world data

Manual parsing vs. the csv module

✦

KEY TAKEAWAY

Learning manual CSV parsing is like learning to drive a manual transmission car before an automatic. You might eventually use the automatic (csv module) most of the time, but understanding what's happening under the hood — reading lines, stripping whitespace, splitting strings — makes you a stronger programmer who can troubleshoot problems and handle unusual file formats with confidence.

SECTION 8

Connection to Advanced Topics

The techniques you learn here form the foundation for more advanced data processing. As your programs grow, you'll encounter larger datasets, messier files, and more complex formats. Here's how today's skills connect to what comes next.

What You Learn NowWhere It Leads
split(",") for simple CSVPython's csv.reader() and csv.DictReader() for robust CSV handling
Lists and dictionariesPandas DataFrames — powerful tables with built-in statistics, sorting, and filtering
Reading text files line by lineParsing JSON, XML, and API responses from web services
Type conversion with int() / float()Data validation, error handling with try/except, and data cleaning pipelines
Manual string parsingRegular expressions for complex pattern matching in text

How this lesson connects to future topics

In a data science or AP Computer Science course, you might use the Pandas library to read a CSV file in a single line: df = pd.read_csv("roster.csv"). Under the hood, Pandas is doing exactly what you've learned — opening the file, reading lines, splitting on delimiters, and building data structures. Knowing the fundamentals means you'll understand what libraries like Pandas are actually doing, and you'll be able to troubleshoot when things go wrong.

SECTION 9

Practice Problems

Work through these five problems to solidify your understanding. Each builds on the previous one, gradually increasing in difficulty. For all problems, assume the CSV files contain simple data with no quoted fields.

Question 1: CONCEPTUAL

Explain why you need to call .strip() on each line before calling .split(","). What would happen if you skipped the .strip() step?
Show answer
Each line read from a file ends with a newline character ( ). If you skip .strip(), the last value in every row will have attached to it. For example, splitting "Alice,16,A\n" on commas would give ["Alice", "16", "A\n"]. The string "A\n" is not the same as "A", so comparisons, printing, and type conversions would behave unexpectedly. The .strip() method removes both the newline and any extra spaces, ensuring clean data.

Question 2: BASIC CALCULATION

Given this line of CSV text: "Mars,6779,687,2\n", write the exact Python expressions to (a) strip the whitespace, (b) split it into a list, and (c) show what the resulting list looks like.
Show answer
Starting with line = "Mars,6779,687,2\n": (a) cleaned = line.strip() produces "Mars,6779,687,2". (b) fields = cleaned.split(",") splits on every comma. (c) The resulting list is ["Mars", "6779", "687", "2"]. Note that all values are strings — "6779" is a string, not the integer 6779. You would need int(fields[1]) to convert it to a number.

Question 3: INTERMEDIATE

Write a complete Python program that reads a file called scores.csv (which has headers name,score) into a list of dictionaries. Then, compute and print the average score across all rows.
Show answer
Here is the complete solution: with open("scores.csv") as f: headers = f.readline().strip().split(",") data = [] for line in f: values = line.strip().split(",") data.append(dict(zip(headers, values))) total = 0 for row in data: total += float(row["score"]) average = total / len(data) print("Average score:", average) The key decisions here are: (1) reading the header first with readline(), (2) using dict(zip(headers, values)) to create dictionaries, and (3) converting the score to a float before adding it to the total.

Question 4: APPLIED

You have a file called weather.csv with headers date,high_temp,low_temp,precipitation. Write a program that reads this file and prints the date(s) where precipitation was greater than 1.0 inches. Also print how many rainy days there were out of the total.
Show answer
Solution: with open("weather.csv") as f: headers = f.readline().strip().split(",") days = [] for line in f: values = line.strip().split(",") days.append(dict(zip(headers, values))) rainy = [] for day in days: if float(day["precipitation"]) > 1.0: rainy.append(day["date"]) print("Rainy days:", rainy) print(f"{len(rainy)} out of {len(days)} days had > 1.0 inches of rain") This problem requires combining CSV parsing with conditional logic. The critical step is converting the precipitation value from a string to a float before comparing it to 1.0. Without that conversion, the comparison would be lexicographic (alphabetical), which gives incorrect results for numbers.

Question 5: CRITICAL THINKING

Consider a CSV file where some rows might be missing a field — for example, a row might read "Dave,,11,2.7" (age is blank). What would split(",") produce for this line? How could you modify your parsing code to handle missing values gracefully, perhaps by substituting a default value like "N/A" for empty strings?
Show answer
Splitting "Dave,,11,2.7" on commas produces ["Dave", "", "11", "2.7"]. The empty string "" is a valid Python string, but it might cause errors if you try to convert it to an integer or float. To handle this, you could add a check after splitting: values = [v if v != "" else "N/A" for v in line.strip().split(",")]. This list comprehension replaces any empty string with "N/A". For numeric fields, you might want to substitute a numeric default like 0 or use a try/except block. This is a real-world concern — messy data with missing fields is extremely common, and knowing how to handle it is an essential skill.

SUMMARY

Lesson Summary

Summary

In this lesson, you learned how to parse CSV files — plain-text files where values are separated by commas — using Python's built-in string methods. The core technique involves three steps: opening the file with a with statement, stripping whitespace from each line using .strip(), and splitting on the comma delimiter using .split(",") to produce a list of string values for each row.

You can store parsed data as a list of lists for quick scripts, or as a list of dictionaries for more readable and maintainable code. To build dictionaries, you use Python's zip() function to pair header names with row values, then wrap the result in dict(). Remember that all values from split() are strings — you must explicitly convert to int() or float() when you need numbers. This manual approach works well for simple CSV files without quoted fields, and it builds essential skills in file I/O and string manipulation that apply far beyond CSV parsing.

Varsity Tutors • Computer Programming 1 (Python) • Parsing CSV Files