GeezerVisonThe World Wide Web, restored to 800 pixels of glory. | ||||||||||||||||||||||
| Home|About|Gaming cabinet|Archives|How it works|Starter sites | ||||||||||||||||||||||
GeezerVison is showing github.com — gh-83273: Rewrite csv.Sniffer dialect detection using trial parsing by serhiy-storchaka · Pull Request #153694 · python/cpython Conversation
|
| sample | old | new |
|---|---|---|
| typical quoted CSV, 4 KB | 0.1 ms | 0.2 ms |
| quoted CSV, 64 KB | 1.5 ms | 2.3 ms |
| quoted CSV, 940 KB | > 20 s | 30 ms |
| single column of quoted fields, 1.1 MB | minutes (gh-98820) | 10 ms |
| random garbage, 1 MB | 9 ms (meaningless answer) | ~25 ms (Error) |
The old regular expressions were quadratic on quoted data; the new code is linear in the sample size with a small factor.
delimiters to list of eligible delimiters #111820: a non-ASCII delimiter in the delimiters argument was ignored.'\r\n' broke detection (the delimiter was detected as a letter).Also addressed, at the triagers' discretion:
sniff("''") now raises csv.Error, so has_header() cannot leak ValueError.Supersedes the stalled PRs gh-109639, gh-103926, gh-153635 and gh-27256, which patch the removed regular expressions. gh-109639 and gh-153635 remain valid as narrow backport candidates for the maintenance branches.
quoting (Feature request: sniff csv quoting and lineterminator #129374) and lineterminator (csv.Sniffer does not detect lineterminator #75008) is deliberately out of scope; the new structure has a natural place for both (_detect_* methods on the winning combination).
Sorry, something went wrong.
There was an error while loading. Please reload this page.
…sing Guess the dialect by parsing the sample with every plausible combination of delimiter, quotechar and escapechar, using the actual CSV parser in strict mode, and choosing the combination which splits the sample into rows with the most consistent number of fields. The old heuristics, which guessed the delimiter from characters adjacent to quotes and from character frequencies, are removed. A large sample is parsed incrementally: first only its beginning, then, after eliminating the combinations which are clearly worse than the leader, a several times larger part, and so on. * csv.Sniffer can now detect escapechar='\\'. * Explicitly requested delimiters are no longer restricted to ASCII. * A delimiter inside a quoted field no longer wins over the actual delimiter, and sniffing no longer takes quadratic time on quoted samples. * The sample can be cut off at an arbitrary point: in the middle of a row, of a quoted field or of an escaped sequence. * Only '\r', '\n' and '\r\n' are treated as row separators, so characters like '\x1c' can now be detected as a delimiter. * A preamble (title or comment lines) before the data does not prevent detection if the data rows outnumber the preamble lines. * A sample consisting of a single column of quoted fields now raises csv.Error instead of guessing a delimiter from the content of the fields, and Sniffer.has_header() no longer raises ValueError for such samples. * doublequote is detected by comparing the two readings of the sample. Co-Authored-By: Claude Fable 5 <[email protected]>
There was an error while loading. Please reload this page.
Documentation build overview
50 files changed ·
|
Sorry, something went wrong.
There was an error while loading. Please reload this page.
I agree with this, let's just do 3.16. This behaviour change will likely benefit from the usual alpha testing phase. |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
# Conflicts: # Lib/csv.py
|
This is based on my 2.5 years old patch. After re-basing it on main and cleaning up debug prints, it was able to fix 3 of existing issues when used as a fallback. When used as a primary method -- 6 more. After fixing few corner cases, the old engine was removed. Then I ordered Claude to implement other my old ideas -- using the real C parser for probing (my initial version was a parser implemented in Python via regexpes) and feeding data by parts to all candidates, with filtering out obvious loosers. This improved performance and also made detection more trustful. |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was an error while loading. Please reload this page.
|
GH-154865 is a backport of this pull request to the 3.15 branch. |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was an error while loading. Please reload this page.
Successfully merging this pull request may close these issues.
CSV sniffing detects wrong field delimiter CSV sniffing falsely detects space as a delimiter csv.Sniffer.snif doesn't set up the dialect properly for a csv created with dialect=csv.excel_tab and containing quote (") char csv.Sniffer().sniff() Hangs on Single-Column Data Csv sniffer doesn't attempt to determine and set escape character.delimiters to list of eligible delimiters
The CSV file delimiter found with csv.Sniffer() is interpreted incorrectly
There was an error while loading. Please reload this page.