Skip to content

fix(csv): take the header from the first non-blank line - #6161

Open
gouyu-hou wants to merge 1 commit into
Tencent:mainfrom
gouyu-hou:fix/csv-header-skip-leading-blank
Open

gouyu-hou wants to merge 1 commit into
Tencent:mainfrom
gouyu-hou:fix/csv-header-skip-leading-blank

Conversation

@gouyu-hou

Copy link
Copy Markdown
Contributor

Observed

A CSV that starts with a blank line produces an empty document:

data = b"\r\nname,age\r\nAlice,30\r\n"
Parser().parse_file("blank.csv", "csv", data, "markitdown").content
Parser returned empty content for file: blank.csv
''

The same file parsed directly with EscapedCSVConverter shows the shape of the problem:

'|  |\n|  |\n|  |\n|  |'

A UTF-8 BOM has the same root cause one row up — it is not stripped, so it lands inside the first header cell:

'| \ufeffname | age |\n| --- | --- |\n| Alice | 30 |'

Expected

| name | age |
| --- | --- |
| Alice | 30 |

Both engines ("", builtin, markitdown) route CSV through the same converter, so all three lose the table.

Root cause

EscapedCSVConverter.convert() takes rows[0] as the header. A leading blank line parses as an empty row, so width = len(rows[0]) is 0, row[:0] is empty, and every rendered cell becomes an empty string — the document body ends up empty and DocReader reports "Parser returned empty content".

The decode keeps a UTF-8 BOM, because stream_info.charset is utf-8 (not utf-8-sig), so \ufeff becomes part of the first header cell.

Files exported by Excel, Numbers and many "download as CSV" endpoints start with a blank line and/or a BOM, so this drops real documents.

Fix

Strip the BOM before splitting the rows, and skip leading rows that have no non-blank cell, so the header is the first row that actually carries data. Rows after the header keep MarkItDown's existing width policy, including intentional blank cells.

Verification

$ python -m pytest docreader/tests/test_csv_cell_boundaries.py -q      # before
SUBFAILED(engine='')        ...test_bom_is_not_part_of_the_first_header_cell
SUBFAILED(engine='builtin') ...test_bom_is_not_part_of_the_first_header_cell
SUBFAILED(engine='markitdown') ...test_bom_is_not_part_of_the_first_header_cell
SUBFAILED(engine='')        ...test_leading_blank_line_does_not_eat_the_table
SUBFAILED(engine='builtin') ...test_leading_blank_line_does_not_eat_the_table
SUBFAILED(engine='markitdown') ...test_leading_blank_line_does_not_eat_the_table
6 failed, 6 passed, 5 subtests passed

$ python -m pytest docreader/tests/test_csv_cell_boundaries.py -q      # after
6 passed, 11 subtests passed

$ python -m compileall -q docreader/parser/csv_converter.py docreader/tests/test_csv_cell_boundaries.py
(no output)

The two new cases cover a leading blank line and a UTF-8 BOM, each across the three engines.

A CSV that starts with a blank line - the shape Excel and many exporters
produce - lost the entire table: the blank line became the header, so the
column width was 0, every cell was rendered as an empty string and the
parser reported an empty document. A UTF-8 BOM survived the decode and
ended up inside the first header cell instead.

Skip the leading blank rows and strip the BOM before the table is built.
Copilot AI balanced review requested due to automatic review settings November 28, 2026 07:26

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota. Try requesting a review at a lower effort level.

This branch has not been deployed

No deployments
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.

2 participants