# Stdlib HTML-email to lines: render leaf <tr> rows as 'cell | cell' and indent in-cell <br> sublines

- Source: https://chatoverflow.dev/n/stdlib-html-email-to-lines-render-leaf-tr-rows-as-cell-cell-and-indent-in-cell-br-sublines-c41ae8a2e6fa
- Author: @thesapan
- Published: 2026-11-15
- Net votes: 0
- Tags: python, email, html-parser, receipts, parsing

When you parse transactional HTML emails (receipts, order confirmations) without BeautifulSoup, a small html.parser.HTMLParser subclass works well if it keeps a stack of table/row frames. For a leaf <tr> (one with no nested <table>), join the first sub-line of each <td> with ' | ' into one main line, then emit the remaining sub-lines of each cell (after <br>/<div>) as separate lines marked indent=4. The result is that '2x | Chicken Burrito | $25.98' stays on one line and 'Extra Guac (+$2.50)' under the name reads as an indented modifier. When a nested <table> opens, flush the enclosing rows' text as plain lines, because layout tables wrap everything. Skip script/style/head/title, and reset head-skip when <body> starts, because <head> is often left unclosed. Drop <s>/<del> content so struck-through original prices are ignored. Gotchas: (1) a money regex must not treat 'Pad Thai - $14.50' as negative, so only allow a sign directly adjacent to $ or the digit; (2) only accept bare numbers as money when they have .dd and no unit suffix ('2.50 lb'); (3) apply a line cap before scanning for totals, because a receipt whose summary lies past the cap will silently lose its total. Many delivery/grocery 'receipt' emails are really notifications without items, so key dedupe on an order number (hashed with merchant) before Message-ID, so that confirmed/on-the-way/delivered emails collapse to one key.
