Parsing
Structured Text
Learn how to read CSV-like data with Python: splitting lines into fields, converting types, and building structured records from raw text.
What is Structured Text?
Structured text is data stored in a consistent, repeating pattern. Each line follows the same format: values separated by a specific character called a delimiter. The most common example is a CSV file (Comma-Separated Values).
Key Vocabulary
Why Parse Structured Text?
Raw text files are everywhere: spreadsheet exports, sensor logs, web API responses, and database dumps. To do anything useful with this data in Python, you need to break each line into individual values you can work with.
The Core Method: .split()
Python's .split() method breaks a string into a list of substrings at each occurrence of the delimiter you specify.
Anatomy of a Parse
.split() vs .strip()
These two methods do very different things, but you almost always use them together. Don't confuse them:
| .split(sep) | .strip() | |
|---|---|---|
| Purpose | Break string into a list at each sep | Remove leading/trailing whitespace |
| Returns | list | string |
| Example | "a,b".split(",") → ['a','b'] |
" hi\n".strip() → 'hi' |
| When to use | Separating fields | Cleaning each field or line |
How to Parse a CSV File
open() and read it line by line..strip()..split(",").int() or float() on numeric fields.Example 1: Basic Parse
Given the file students.csv with this content:
Example 2: Type Conversion
Now we convert each record into a dictionary with proper types:
Example 3: Tab-Delimited Data
Not all files use commas. Some use tabs (\t) or pipes (|). Just change the delimiter in .split():
Building a Reusable Parser
Wrap the parsing logic in a function so you can reuse it with any file and delimiter:
Processing After Parsing
Once you have a list of records, you can filter, compute, and report:
Common Mistakes
\n. Without .strip(), your last field will have a hidden newline character..split(), everything is a string. "17" + "11" gives "1711", not 28..split(",") on tab-separated data returns the entire line as one field.Parsing Cheat Sheet
| Method | Input | Output | Purpose |
|---|---|---|---|
.strip() |
" hi\n" | "hi" | Clean whitespace |
.split(",") |
"a,b,c" | ['a','b','c'] | Separate fields |
int() |
"17" | 17 | String to integer |
float() |
"3.14" | 3.14 | String to decimal |
zip() |
headers, values | paired tuples | Match header to value |
Question 1 of 5
What does "a,b,c".split(",") return?
.split(",") breaks a string at each comma and returns a list of substrings. Parentheses would indicate a tuple, and quotes alone would be a single string.Question 2 of 5
After splitting "Alice,17,11" on ",", what is the type of the second element?
.split() always returns a list of strings. The element '17' looks like a number but is still a str until you convert it with int().Question 3 of 5
Which line of code correctly removes the trailing newline and splits on commas?
.strip() first (on the string) to remove whitespace, then .split(",") to break it into fields. Option B fails because .split() returns a list, and lists don't have a .strip() method.Question 4 of 5
Consider this file scores.csv. You want to compute the average score.
Which conversion function should you use on the score field?
int("95.5") would raise a ValueError. You must use float() to handle decimal numbers.Question 5 of 5
Given this code:
What will print(len(parts)) output?
\t), but the code splits on commas. Since there are no commas in the string, .split(",") returns a list with the entire string as one element, so len(parts) is 1.