The Cleaner Table
Hoppy pulls a small CSV table out of the data cabinet. The rows and columns are already there, but every cell still has dust around the edges: some fields have extra spaces, some carry a tiny noisy symbol at the start or end, and the whole table feels almost usable instead of truly ready.
That is exactly today's point: taking the string-cleaning moves from Chapter 1 and using them on a real record file. We are not polishing one string anymore. We are repeating the same light cleanup across multiple rows in a table.
If you can clean one cell, you can clean the table row by row
Back in Chapter 1, strip() helped you brush the edges of a string. The move itself has not changed. The setting is just more realistic now: once each table row is broken into fields, you repeat that same light cleanup on each field.
raw_cells = [" moss compass ", " *drawer-2* ", " keeper?? "]
clean_cells = []
for cell in raw_cells:
clean_cells.append(cell.strip(" *?"))
print("Clean cells:", clean_cells)
This is still not the full task for today. It does not read a file or rebuild complete CSV rows. It only shows the key instinct first: one cleanup move can be applied again and again across a group of values.
Today's task: clean the messy fields in cleaner_table.csv one row at a time
The starter already reads cleaner_table.csv for you, stores the header in header_line, and keeps the real data rows in dirty_rows. Your job is to split each row, clean it, rebuild a tidy CSV row, and collect the results in cleaned_rows.
Run the starter once and inspect the header plus the unfinished data rows. You will notice that each row is still just comma-separated text.
Use a loop over dirty_rows. For each row, call row.split(",") to break it into three fields, then do a small cleanup on each field.
The noise in this tiny table is deliberately light: only edge spaces and a few *, !, #, and ? marks. A call like strip(" *!#?") is enough to brush those off the ends.
Join the three cleaned fields back into one comma-separated string, then append it to cleaned_rows. When you run again, the output should show the tidy version of the table rows.
Today is only about light table cleanup: loop by row, split into fields, tidy the field edges, and print the cleaned result. We are not handling complex broken rows, large datasets, or any data analysis.
Suggested SolutionExpandCollapse
with open("cleaner_table.csv", "r", encoding="utf-8") as file:
lines = file.read().splitlines()
header_line = lines[0]
dirty_rows = lines[1:]
print("Header:", header_line)
print("Dirty rows:", dirty_rows)
cleaned_rows = []
for row in dirty_rows:
item_name, keeper_name, table_slot = row.split(",")
clean_item_name = item_name.strip(" *!#?")
clean_keeper_name = keeper_name.strip(" *!#?")
clean_table_slot = table_slot.strip(" *!#?")
cleaned_row = clean_item_name + "," + clean_keeper_name + "," + clean_table_slot
cleaned_rows.append(cleaned_row)
print("Cleaned rows:", cleaned_rows)Advanced TipsWant more? Click to expandClick to collapse
The main feeling to keep from this lesson is that your earlier string moves did not belong only to tiny single-string exercises. Once real record files appear, those same moves show up again and again and help you slowly make a table usable.
In the next lesson, Hoppy will keep moving forward: not only cleaning a record, but noticing that plain text and structured records can be turned into each other.