Skip to main content

Python Programming

Build a stock-count importer that can explain a bad row

Read a quoted CSV file, validate its shape and quantities, and stop a bad batch before it replaces a stock snapshot.

Try the 5-minute exercise

A shop sends a stock snapshot with one record per SKU. A blue pen has the printed SKU 007 and the catalog name “Pen, blue”. Last week a helper split each line at commas and converted every numeric-looking field to an integer. The pen acquired the wrong name and became indistinguishable from SKU 7. Both mistakes occurred before anyone checked the stock count.

Give each column a contract

For this importer the header must be exactly sku, name, quantity in that order. SKUs and names are text. A quantity is a nonnegative count written with ASCII digits. Zero means somebody checked an empty shelf. A blank cell means the count is missing. The shop also requires at least one product in a replacement snapshot.

import csv
from io import StringIO

def preview(file):
    rows = csv.reader(file, strict=True)
    if next(rows, None) != ["sku", "name", "quantity"]:
        raise ValueError("Expected sku,name,quantity")
    products = {}
    for record, fields in enumerate(rows, start=2):
        if len(fields) != 3:
            raise ValueError(f"Record {record}: expected three fields")
        sku, name, quantity = (field.strip() for field in fields)
        if not sku or not name or not (quantity.isascii() and quantity.isdecimal()):
            raise ValueError(f"Record {record}: check SKU, name and quantity")
        if sku in products:
            raise ValueError(f"Record {record}: repeated SKU {sku}")
        products[sku] = (name, int(quantity))
    if not products:
        raise ValueError("Snapshot contains no products")
    return products

data = 'sku,name,quantity\n007,"Pen, blue",4\n7,Pad,0\n'
assert preview(StringIO(data)) == {"007": ("Pen, blue", 4), "7": ("Pad", 0)}

Rehearse a failed import

Append a record for 007 with a different quantity. The preview raises an error naming record 4. With a quoted product name that spans lines, a CSV record can occupy multiple physical lines, so report the unit you actually count. When reading a real Python text file, open it with the agreed encoding and newline="" so the CSV parser handles newline conventions. A malformed quoted record can raise csv.Error; the caller should report that as a failed preview too.

Only the preview dictionary changes while the file is being read. In an application, commit the replacement in a database transaction after the preview succeeds. Put a bad record last in a test and assert that the previous snapshot is still available. That checks the promise the shop cares about: a failed delivery cannot leave Tuesday’s counts mixed with Monday’s. Python CSV reference

Put it into practice · 5 minutes

Read a product name containing a comma

Try the exercise and get feedback on your answer.

Try the exercise