Understand the file contract
A text file stores characters. A newline ends a row in this exercise and a comma separates fields. For A12,4, the team code remains text and the points field is converted after validation. Decide in advance whether the file has a header, how many fields are expected, and whether spaces should be stripped. These lessons use headerless files without quoted fields. A real CSV can contain quoted commas, so a general parser must handle those rules rather than blindly splitting every comma.
The file path is part of the program's input. On a computer, open("points.csv") looks in the current working directory. An IDE may use the project folder or a configured working directory. If Python reports FileNotFoundError, check the actual filename, including the extension, and the folder the program is running from. In the browser lab the starter creates the file in the virtual workspace. Downloaded files must be placed alongside each other for the local practice task, or opened with the correct path.
Trace a report safely
| Raw row | After split | Valid? | Report row |
|---|---|---|---|
A , 06 | A and 06 after trimming | Yes | A,6 |
B, | B and empty text | No | No row |
C,7,extra | Three fields | No | No row |
D,21 | D and 21 | No, range | No row |
Check len(fields) before accessing a field by index. Remove surrounding whitespace from each field where the brief permits it. Reject an empty identifier. For points, test .isdecimal() before int(), then compare the integer with the limit. When writing the result, use the cleaned identifier and the converted number to produce the required format. A newline written after each record ensures that the next one starts on a separate line.
Choose the correct mode and scope
Use read mode when you want to inspect existing data. Use write mode for a replacement report. It clears the old content immediately on opening, so give input and output different filenames. Append mode is for a growing log, where previous entries should stay. A task that says “produce today's report” and a task that says “add this record to the history” often need different modes.
A with block closes the file when control leaves the block. Open the output once, outside the processing loop. Opening it in write mode on every pass would repeatedly clear the earlier accepted rows. Opening it only inside an if that detects a valid row can leave yesterday's report untouched when today's input has no valid rows. A strong test checks two successive runs, including one that should produce an empty output file.
In the browser, the Test input first supplies the row count and then the raw rows. The starter writes them into the named CSV. The rest of your program should read from that file. Run with my input lets you try a particular malformed row before running the full set. The output check shows what was read back from the generated report. In a local IDE, open the saved report itself as well. The output panel alone cannot tell you which file was written.
Practise a Paper 2 change request
A provided stock.csv contains one item code and a quantity per line. Create a new restock.csv with the codes of items whose quantity is below 4. A valid row has a non-empty code and a whole-number quantity of 0 or more. Preserve input order. Use a file with rows A,0, B,4, C,3, D,broken and E. Predict the contents of the report, including whether there should be a line for B. Then test an empty input file and a file that contains only invalid rows.