fix: auto-detect CSV delimiter via Sniffer in import_set and detect()#639
Open
terminalchai wants to merge 1 commit into
Open
fix: auto-detect CSV delimiter via Sniffer in import_set and detect()#639terminalchai wants to merge 1 commit into
terminalchai wants to merge 1 commit into
Conversation
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## master #639 +/- ##
==========================================
+ Coverage 93.14% 93.18% +0.03%
==========================================
Files 29 29
Lines 3226 3256 +30
==========================================
+ Hits 3005 3034 +29
- Misses 221 222 +1 ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
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.
Fixes #622
Problem
tablib.Dataset().load(data, format='csv')always uses,as the delimiter, even when the file uses;,:,|, or other separators. This is becauseimport_setcallskwargs.setdefault('delimiter', cls.DEFAULT_DELIMITER)without ever sniffing the actual content.A secondary issue:
CSVFormat.detect()passeddelimiters=delimiter or cls.DEFAULT_DELIMITERtocsv.Sniffer().sniff(), which restricted sniffing to comma-only. This meant non-comma CSV files were never recognised during format auto-detection.Fix
import_set: when no
delimiterkwarg is provided, read up to 2048 bytes as a sample, callcsv.Sniffer().sniff(sample)with no delimiter restriction, and use the detected delimiter. The stream is then seeked back to 0 before the actual read. A guard rejects alphabetic/digit detections (the Sniffer occasionally misidentifies a letter on very short or ambiguous samples). Falls back toDEFAULT_DELIMITERon anycsv.Error.detect(): when no explicit delimiter is given, uses a candidate string of common non-tab separators (',;:|') for CSVFormat, and the format's
DEFAULT_DELIMITERfor subclasses (e.g. TSVFormat uses tab). This keeps tab-delimited files out of CSV auto-detection while still recognising;,:, and|separated files.Explicit
delimiterkwargs are fully respected and take precedence.Tests
test_csv_formatter_support_kwargs: old assertion documented the broken behaviour (1 header instead of 3); updated to assert correct parsing.test_csv_import_set_auto_detect_delimiter: colon- and pipe-delimited CSV auto-detection.test_csv_detect_non_comma_delimiter:detect()correctly recognises:,|, and;separated files.All 155 tests pass.