Skip to content

fix(csv): strip the UTF-8 BOM and skip blank rows before building the table - #2303

Open
Ruiming Zhao (uuzzrm) wants to merge 1 commit into
microsoft:mainfrom
uuzzrm:fix/csv-bom-and-blank-rows
Open

fix(csv): strip the UTF-8 BOM and skip blank rows before building the table#2303
Ruiming Zhao (uuzzrm) wants to merge 1 commit into
microsoft:mainfrom
uuzzrm:fix/csv-bom-and-blank-rows

Conversation

@uuzzrm

Copy link
Copy Markdown

Two things broke real-world CSV conversion:

  1. UTF-8 BOM in the header. Excel and other tools prepend a BOM to CSV exports. The converter decoded it but never removed it, so the invisible character ended up inside the first header cell (| \ufeffname | age |).
  2. Blank lines shifted or destroyed the table. csv.reader parses a blank line as an empty row. A trailing blank line added a garbage | | row, and a leading blank line was worse: the empty row became the header, so | | + a zero-column separator was emitted and every real column was truncated away - the whole table came out as | | lines.

This change strips the BOM and drops empty rows before deciding what the table looks like. The existing behavior for genuinely empty input is unchanged (empty markdown).

Tests: six new cases in tests/test_csv_converter.py covering the BOM with and without a charset hint, leading/trailing/mid-file blank lines, and all-blank input. They fail against the previous code (the blank-line ones produced the broken tables above) and pass with the fix. ruff check, ruff format --check, and mypy are clean on the changed files.

Note: cell escaping (pipes / embedded newlines inside quoted fields) is a separate issue, already covered by #1816 - this PR intentionally stays out of that.

… table

Two things broke real-world CSV conversion: Excel prepends a UTF-8 BOM that landed inside the first header cell, and a single leading or trailing blank line parsed as an empty row and shifted every column (a leading blank line destroyed the table entirely, since the empty row became the header). Strip the BOM and drop empty rows before deciding what the table looks like.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant