The Comma Sheets
Hoppy opens another drawer in the data cabinet and finds not a JSON scroll, but a few cargo sheets separated by commas. At first they look like ordinary text lines, but with one more look you can tell that each line behaves like a record and each column carries one kind of detail.
Today's goal is simple: build a first instinct for CSV. It is not just one long string. It is a text format arranged by rows and columns.
CSV looks like text, but its shape feels like a tiny table
CSV stands for comma-separated values. A good first picture is this: each line is a record, each column is a field, and the first line is often a header that tells you what each column means.
mini_sheet = """crate_id,item_name
A-1,fern rope
A-2,moon lamp"""
lines = mini_sheet.splitlines()
header = lines[0].split(",")
first_row = lines[1].split(",")
print("Header:", header)
print("First item:", first_row[1])
The key thing to keep is not a big API list. It is the shape: the first line gives column names, and the lines after that are real records. The commas simply mark where one column stops and the next one begins.
Today's task: inspect the raw CSV, then pull out the header and the first record
The starter already reads cargo_sheet.csv and prints the raw text for you. Your job is to break that small table into clearer pieces: the header row, the first data row, and two fields from that first record.
Run the starter and stare at cargo_sheet.csv for one moment. Notice where the new lines are and where the commas are. You should see that CSV is text, but text with a table-like shape.
The starter already gives you rows. Set header_columns to the columns from the first line, then set first_row_columns to the columns from the first real record.
Use first_row_columns to read first_crate_id and first_item_name. That way you are not only looking at the whole table shape; you are also pulling one concrete record out of it.
Today is only about the CSV shape: rows, columns, the header row, and reading one simple record. We are not using Pandas, not touching Excel automation, and not doing messy-data cleanup yet.
Suggested SolutionExpandCollapse
with open("cargo_sheet.csv", "r", encoding="utf-8") as file:
csv_text = file.read()
print("Raw CSV text:")
print(csv_text)
rows = csv_text.strip().splitlines()
header_columns = rows[0].split(",")
first_row_columns = rows[1].split(",")
first_crate_id = first_row_columns[0]
first_item_name = first_row_columns[1]
print("Header columns:", header_columns)
print("First row columns:", first_row_columns)
print("First crate id:", first_crate_id)
print("First item name:", first_item_name)Advanced TipsWant more? Click to expandClick to collapse
The main instinct from today is that CSV may be plain text, but it is not random text. Once you can see records by row, columns by comma, and the first line as the header, you have started to read the table instead of just staring at a long string.
In the next lesson, Hoppy will meet a slightly messier table, where a few fields need light cleanup before the records feel trustworthy.