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.
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")))(afterimport 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 givesNone; 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 withre.fullmatch(r"-?[0-9]+(\.[0-9]+)?", s)(afterimport re); if it is not, returnNone; otherwisefloat(s), made negative if it was in brackets.Nonemeans β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 isNonego intobad_rowsand are skipped withcontinue. - Totals. Add the readable amounts to
totaland toby_account(the count-by-key pattern, adding the amount). Round withround(x, 3)at the end, so that tiny floating-point errors do not show. - Fill in
parse_amount,convert(req)(the list ofparse_amountof each text), thenn_rows,total,by_accountandbad_rows. The checks runconverton texts that you cannot see: brackets, a lone$,1.2.3,-0,$-5.00and an empty list.
The lab gives you the data for this case, invented and generated for you alone.
Preparing your dataβ¦
Chidi: βYou found the lines that cannot be read: they are the difference. But is skipping them the right thing to do in a report?β
π The debrief unlocks when you close the case. Finish the lab above.
The debrief
Chidi: βSo skipping is a decision, and it has to be shown.β
What an experienced person would do next. Never skip silently: report how many lines were read and how many were not, with the total of what was read next to it, so that a reader knows how much of the ledger the number covers. Send the list of unreadable lines to a person to fix at the source, and fix the cause (a form that accepts only numbers). Keep the raw text of the bad lines in the report, so that nobody has to hunt for them. For money, real programs use whole kobo (integers) or the decimal module, never floats, to avoid rounding drift; here we round at the end to keep the case simple. And remember that βn/aβ and an empty cell may mean different things to the person who typed them.
The trap. Converting with float(text) and letting the first bad value crash the program, or wrapping it in a bare try that hides every problem.
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).
π§ͺ Want to practise this on your own computer? See how to set up your lab →