feat: add TSV support and delimiter auto-detection to CsvConverter#2021
Open
anandmt wants to merge 1 commit into
Open
feat: add TSV support and delimiter auto-detection to CsvConverter#2021anandmt wants to merge 1 commit into
anandmt wants to merge 1 commit into
Conversation
Extend CsvConverter to handle TSV files alongside CSV by accepting .tsv extension and text/tab-separated-values MIME type, and using csv.Sniffer to auto-detect delimiters with sensible fallbacks. Also escape pipe characters and newlines in cell values to prevent broken Markdown tables.
|
@anandmt please read the following Contributor License Agreement(CLA). If you agree with the CLA, please reply with the following information.
Contributor License AgreementContribution License AgreementThis Contribution License Agreement (“Agreement”) is agreed to by the party signing below (“You”),
|
| "text/csv", | ||
| "application/csv", | ||
| "text/tab-separated-values", | ||
| "text/tsv", |
There was a problem hiding this comment.
I couldn't find text/tsv as IANA recognized MIME type why has this been added? IANA
|
|
||
| return DocumentConverterResult(markdown=result) | ||
|
|
||
| def _detect_delimiter(self, content: str, extension: str) -> str: |
There was a problem hiding this comment.
After doing a little more digging I have found that this implementation is fairly brittle in my opinion.
- using the
csv.snifferfunction for checking for delimiter is highly brittle as it deduces the delimiter using a heuristic methods (like frequency counting) it tends to produce false positives and false negative. you don't check if the guess is correct or not. docs - If case of
csv.Error, you only check if the file's extension is ".tsv" but in the case where the file are coming from Object Storage (AWS S3, Google Cloud Storage), these files usually don't have any extension at all, hence checking the MIME type along with extension is better way to fallback. currently all files without extension during csv.Error simply fallback to CSV type.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
CsvConverterto accept.tsvfiles andtext/tab-separated-values/text/tsvMIME typescsv.Snifferwith sensible fallbacks (tab for.tsv, comma otherwise)|) and newlines in cell values to prevent broken Markdown tables (also addresses [BUG]: CsvConverter produces broken Markdown tables when cell values contain pipe characters (|) #2019)Motivation
TSV (Tab-Separated Values) files are extremely common in data science, bioinformatics, and LLM pipelines, but are not currently supported by MarkItDown. This is a natural extension of the existing CSV converter.
Changes
_csv_converter.py: Added.tsvextension and TSV MIME types to accepted formats. Added_detect_delimiter()usingcsv.Snifferfor auto-detection with fallbacks. Added_escape_cell()to escape pipe characters and newlines in cell values.test_files/test.tsv: New test file with tab-delimited data including a pipe character in a cell value._test_vectors.py: NewFileTestVectorfor TSV conversion withmust_includechecks.Test plan
convert_local,convert_stream_with_hints,convert_stream_without_hints,convert_file_uri,convert_data_uri)test_mskanji.csv) continues to pass — delimiter auto-detected as comma\|in output