beginner

Case File: The Ledger Export

The accounts spreadsheet was exported to a file in which people typed the amounts in many styles: plain, with a dollar sign, in quotes with thousands commas, in brackets for a loss, empty, or the text n/a. Read it with the csv module, write a careful function that turns text into a number or None, total what can be read, total by account, and list the lines that could not be read. Learn that every value in a file is text. The ledger is generated for you alone.

45 min

Sabi Logistics Β· Python 6

The Ledger Export

The month-end total does not match the bank, and the difference is exactly the lines that the spreadsheet silently skipped

Chidi Engineering manager Β·

Chidi. The month-end total of the depot ledger is different from the bank statement, and the finance team thinks that the spreadsheet silently skipped some lines. I do not want to guess. I exported the ledger to a file: a date, an account, an amount and a note.

Please read it, total what you can read, give me the total for each account, and list the line numbers that you could not read, so that a person can look at exactly those lines. The amounts were typed by hand, so expect all kinds of styles: dollar signs, commas, brackets for losses, empty cells, and β€œn/a”.

The brief

What you need to know first.

  • Reading a CSV. rows = list(csv.DictReader(open("ledger.csv"))) (after import csv) gives one dictionary per data line, using the first line as the names of the columns: row["amount"] is the text of the amount. Everything that comes from a file is text, even what looks like a number, so you must convert it yourself.
  • A careful converter. Write parse_amount(text): strip the spaces; an empty text gives None; a text in brackets means a loss, so remember that and remove the brackets; remove a leading $ and any commas; check that what is left is a number with re.fullmatch(r"-?[0-9]+(\.[0-9]+)?", s) (after import re); if it is not, return None; otherwise float(s), made negative if it was in brackets. None means β€œI could not read this”, and it lets the rest of the program decide.
  • Line numbers. for number, row in enumerate(rows, start=1): gives each row with its number, counting from 1, which is how a person counts lines of data. Rows whose amount is None go into bad_rows and are skipped with continue.
  • Totals. Add the readable amounts to total and to by_account (the count-by-key pattern, adding the amount). Round with round(x, 3) at the end, so that tiny floating-point errors do not show.
  • Fill in parse_amount, convert(req) (the list of parse_amount of each text), then n_rows, total, by_account and bad_rows. The checks run convert on texts that you cannot see: brackets, a lone $, 1.2.3, -0, $-5.00 and an empty list.

The lab gives you the data for this case, invented and generated for you alone.

Preparing your data…

πŸ”’ The debrief unlocks when you close the case. Finish the lab above.

Write the ledger note

Write the note for the engineering manager and finance: how the file was read (csv.DictReader), what the converter accepts and returns None for (empty, n/a, text that is not a number, a lone dollar sign), the total of what was read, the total of each account, the number of the lines that could not be read and how many that is out of all the lines, why the total differs from the bank, and what to do (report the unreadable lines to a person, fix the source, keep the raw text, never skip silently, whole kobo for money).

0 words

πŸ§ͺ Want to practise this on your own computer? See how to set up your lab →

πŸ€–

Need clarification? Ask GT

Hi, I'm GT β€” your AI tutor for Case File: The Ledger Export. What would you like to know?
πŸ“

Knowledge Check

1 of 3

What type does a value read from a CSV file have?

βš‘ Capture The Flag

Report a problem

About: /lessons/case-the-ledger-file