Your parse_customer_csv works for clean CSV, but what if a customer name contains a comma — like 'Smith, Jane' — or a field is quoted? Your split(',') would break.
After parse_customer_csv I have the basic parser. But you're right — 'Smith, Jane' in a company name would split into two fields. Every real Stripe export has edge cases like that.
Python's csv module handles all of that automatically. csv.DictReader reads a file-like object and maps each row to a dict using the header row as keys. io.StringIO wraps a string to look like a file — so you can pass CSV text directly without touching the disk:
import csv, io
reader = csv.DictReader(io.StringIO(csv_text))
for row in reader:
print(dict(row)) # each row is already a named dictSo io.StringIO is just a fake file? It wraps the string so csv.DictReader thinks it's reading from disk?
Exactly — it's a file-like interface over a string. csv.DictReader doesn't care whether it's reading a real file or a StringIO — it sees the same interface. Here's the full function:
def load_customers_from_csv(csv_text: str) -> list:
import csv, io
customers = []
reader = csv.DictReader(io.StringIO(csv_text))
for row in reader:
customers.append({
"name": row.get("name", "").strip(),
"plan": row.get("plan", "unknown").strip(),
"mrr": float(row.get("mrr", 0)),
"seats": int(row.get("seats", 1))
})
print(f"Loaded {len(customers)} customers")
return customersSo load_customers_from_csv handles quoted fields, commas in names, and all the edge cases my manual parser missed — by using the standard library's battle-tested CSV reader.
csv.DictReader has handled every CSV edge case since Python 2.3. Your split(',') is reinventing a very lumpy wheel.
I replaced my manual CSV parser with the standard library in five lines. Real exports — quoted fields, odd characters — all handled automatically.
Always import from the standard library first before writing manual parsing. csv, json, re, collections — they exist because these problems are solved. Your job is to call them, not recreate them.
csv.DictReader reads CSV rows as dicts keyed by the header row.
import csv, io
reader = csv.DictReader(io.StringIO(text))
for row in reader:
print(dict(row)) # {'name': 'Acme', 'mrr': '149.0', ...}| Feature | split(',') | csv.DictReader |
|---|---|---|
| Quoted fields | Breaks | Handled |
| Commas in values | Breaks | Handled |
| Unicode | Manual | Built-in |
DictReader values are always strings. Still cast numeric fields: float(row['mrr']).
Your parse_customer_csv works for clean CSV, but what if a customer name contains a comma — like 'Smith, Jane' — or a field is quoted? Your split(',') would break.
After parse_customer_csv I have the basic parser. But you're right — 'Smith, Jane' in a company name would split into two fields. Every real Stripe export has edge cases like that.
Python's csv module handles all of that automatically. csv.DictReader reads a file-like object and maps each row to a dict using the header row as keys. io.StringIO wraps a string to look like a file — so you can pass CSV text directly without touching the disk:
import csv, io
reader = csv.DictReader(io.StringIO(csv_text))
for row in reader:
print(dict(row)) # each row is already a named dictSo io.StringIO is just a fake file? It wraps the string so csv.DictReader thinks it's reading from disk?
Exactly — it's a file-like interface over a string. csv.DictReader doesn't care whether it's reading a real file or a StringIO — it sees the same interface. Here's the full function:
def load_customers_from_csv(csv_text: str) -> list:
import csv, io
customers = []
reader = csv.DictReader(io.StringIO(csv_text))
for row in reader:
customers.append({
"name": row.get("name", "").strip(),
"plan": row.get("plan", "unknown").strip(),
"mrr": float(row.get("mrr", 0)),
"seats": int(row.get("seats", 1))
})
print(f"Loaded {len(customers)} customers")
return customersSo load_customers_from_csv handles quoted fields, commas in names, and all the edge cases my manual parser missed — by using the standard library's battle-tested CSV reader.
csv.DictReader has handled every CSV edge case since Python 2.3. Your split(',') is reinventing a very lumpy wheel.
I replaced my manual CSV parser with the standard library in five lines. Real exports — quoted fields, odd characters — all handled automatically.
Always import from the standard library first before writing manual parsing. csv, json, re, collections — they exist because these problems are solved. Your job is to call them, not recreate them.
csv.DictReader reads CSV rows as dicts keyed by the header row.
import csv, io
reader = csv.DictReader(io.StringIO(text))
for row in reader:
print(dict(row)) # {'name': 'Acme', 'mrr': '149.0', ...}| Feature | split(',') | csv.DictReader |
|---|---|---|
| Quoted fields | Breaks | Handled |
| Commas in values | Breaks | Handled |
| Unicode | Manual | Built-in |
DictReader values are always strings. Still cast numeric fields: float(row['mrr']).
Omar needs a robust CSV parser for Stripe exports that handles quoted company names with commas and special characters. Write `load_customers_from_csv(csv_text)` that uses `csv.DictReader` and `io.StringIO` to parse the text into a list of customer dicts, casting mrr to float and seats to int.
Tap each step for scaffolded hints.
No blank-editor panic.