1. Lighthouse

Level 5 · Boss: parse the supplier export

The request

From Laura Bennett, Head of Finance

“Our supplier invoice tool exports prices as text with dollar signs, commas and stray spaces. I need real numbers so I can check an invoice total, and receipt lines I can paste into an email.”

Your goal

Step 1 of 3. Define parse_price(text) that strips spaces, removes '$' and ',' and returns a float. Set result to [parse_price(t) for t in raw_prices].

  • Store a list of 4 in resultnot done yet

Step 1 · your code

Python

Loading the editor…

    ResultPress Run to see yours

    Mote checks your result

    Press Run. Each row that matches the goal becomes a firefly in your lantern.

    Step 2 · your code

    Python

    Loading the editor…

      ResultPress Run to see yours

      Mote checks your result

      Press Run. Each row that matches the goal becomes a firefly in your lantern.

      Step 3 · your code

      Python

      Loading the editor…

        ResultPress Run to see yours

        Mote checks your result

        Press Run. Each row that matches the goal becomes a firefly in your lantern.

        The lesson

        Text that should be numbers

        Exports from invoicing tools, spreadsheets and web pages often store numbers as formatted text: "$1,249.00", " 34.50", "12,000". You can’t add those up. float("$1,249.00") fails with ValueError: could not convert string to float. Before any analysis, the text has to become a number, and the cleanest way to do that is a small, well-tested function.

        Step 1: one job, one function

        def parse_price(text):
            cleaned = text.strip().replace("$", "").replace(",", "")
            return float(cleaned)

        Each method returns a new string, so the chain reads left to right: trim the spaces, drop the dollar sign, drop the thousands separators, then convert. Test it on the awkward cases, like a leading space or a five-digit amount, before trusting it. If a value can’t be parsed (say "N/A"), float() raises an error, and that’s usually what you want: a loud failure beats a silently wrong total.

        Step 2: functions inside expressions

        Once parse_price exists, the total is one expression. Each invoice line is a tuple of three values, which you can unpack right in the comprehension:

        sum(qty * parse_price(price) for name, qty, price in invoice)

        This is composition: small functions used as building blocks inside bigger expressions. Don’t copy the .strip().replace(...) chain into the total. If the export format changes (a currency code appears, say), you want to fix exactly one place.

        The name name isn’t used in the total, but unpacking needs a variable for every position. Python convention is to call an unused variable _, as in for _, qty, price in invoice.

        Step 3: formatting for people

        Analysts produce numbers for people, and people read $1,249.00 far more easily than 1249.0. The format spec ,.2f adds a thousands separator and fixes two decimals:

        f"{1249.0:,.2f}"      # '1,249.00'
        f"{138.0:,.2f}"       # '138.00'

        Keep the number as a number until the very last moment, then format. A list of formatted strings is great for an email, but you can’t sum it any more.

        A small helper like receipt_line(name, qty, price_text) keeps the comprehension short and gives the formatting rule a name. That pays off the day someone asks for the same line in a different report.

        Habits worth keeping

        • Parse once, at the edge of your code, then work with clean numbers.
        • Keep raw and cleaned values separate until you’ve checked the cleaning.
        • Cross-check: the line totals from step 3, parsed back to numbers, should add up to the step 2 total.

        Common mistakes

        • Removing the comma but not the dollar sign (or the other way round), so float() fails.
        • Forgetting .strip(). float(" 34.50") actually tolerates surrounding spaces, but other cleaning steps such as checking startswith("$") do not, so strip first.
        • Using {line_total:.2f} without the comma, which prints 1249.00 instead of 1,249.00.