Reading a file
By the end of this step your program reads Ridgeline's real descriptions file and works with every row in it.
A CSV is a spreadsheet saved as text
Marketing's descriptions arrive as a CSV file ("comma-separated values"). It's a spreadsheet saved as plain text: one line per row, commas between the columns. The first line is the header with the column names. Here are the first lines of descriptions.csv:
sku,description
RL-COO-1001,"Recycled fabrics where it counts, tough ones where it matters. …"
RL-COO-1002,"Light enough for thru-hikes, sturdy enough for base camp. …"Two columns: sku (Ridgeline's product code) and description. Some descriptions contain commas themselves, so they're wrapped in quotes to show where they start and end.
Download descriptions.csv if you'd like to open it in a spreadsheet app and look. You'll need it in the next step anyway.
Reading it
Reading a file is three moves in any language: open it, read the text, turn the text into rows you can loop over.
- Python has a built-in helper,
csv.DictReader, that turns each row into a record:row["sku"],row["description"]. It also handles the quotes for you. - JavaScript in the browser here gets the file as one big piece of text. The code splits it into lines, then splits each line at its first comma. (Real projects use a ready-made CSV library; this keeps the moving parts visible.)
Both boxes below already have the real file loaded. Run them.
Output
Press Run (or Ctrl+Enter) to see the output here.
Output
Press Run (or Ctrl+Enter) to see the output here.
New bits in the Python version:
import csvbrings in Python's CSV helper.importis how you use code that someone else wrote.with open(...) as f:opens the file and closes it again automatically when the indented block ends.list(...)turns all the rows into a list, so you can count them and loop over them.rows[:3]is "the first three rows";row["description"][:60]is "the first 60 characters of the description".
Your turn
Before you build the checker: what should your program do when a description is empty?