Parse the supplier price-list CSV

Python · Python · beginner · greenfield

Adds the price-list parser for the nightly catalog sync. Splits the file into lines, drops the header, unpacks each row into its four fields (stripping the exporter's quoting, including doubled inner quotes), and converts the numeric columns. Ran it over a sample file from the supplier portal and the catalog rows come out typed correctly.

Nightly job syncing ~12k products. If the sync aborts, the storefront keeps selling at yesterday's prices.

Requirements

Files touched

--- pricing/price_list.py
+"""Parse the supplier's price-list CSV into catalog rows.
+
+Header: sku,name,unit_price_cents,stock
+"""
+
+
+def _strip_quotes(field: str) -> str:
+    if field.startswith('"') and field.endswith('"') and len(field) >= 2:
+        return field[1:-1].replace('""', '"')
+    return field
+
+
+def parse_price_list(text: str) -> list[dict]:
+    lines = [line for line in text.splitlines() if line.strip()]
+    rows = []
+    for line in lines[1:]:
+        sku, name, price_cents, stock = (_strip_quotes(f) for f in line.split(","))
+        rows.append(
+            {
+                "sku": sku,
+                "name": name,
+                "unit_price_cents": int(price_cents),
+                "stock": int(stock),
+            }
+        )
+    return rows
+

Review this PR

Python practice