Sniffing
tabbed.sniffing
Tools for determining the dialect and structure of a csv file that may contain metadata, a header, and a data section.
tabbed.sniffing.Sniffer
Bases: ReprMixin
A tool for inferring the dialect and structure of a CSV file.
The formatting of CSV files can vary widely. Python's builtin Sniffer is capable of handling different dialects (separators, line terminators, quotes etc) but assumes the first line within the file is a header or a row of unheaded data. In practice, many CSV files contain metadata prior to the header or data section. While these files are not compliant with CSV standards (RFC-4180), their broad use necessitates file sniffing that infers both dialect and structure. To date, some csv readers such as Pandas read_csv allow metadata rows to be skipped but no formal mechanism for sniffing dialect, metadata and header information exist. This Sniffer supports these operations.
Attributes:
Name | Type | Description |
---|---|---|
infile |
An open file, an IO instance. |
|
line_count |
The number of lines in infile. |
|
start |
int
|
The start line of infile for collecting a sample of 'amount' number of lines. |
amount |
int
|
The number of infile lines to sample for dialect, header and metadata detection. The initial value defaults to the smaller of line_count or 100 lines. The amount should be large enough to include some of the data section of the file. |
skips |
List[int]
|
Line numbers to ignore during sample collection. |
Examples:
Source code in src/tabbed/sniffing.py
90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 |
|
start
property
writable
Returns the start line of this Sniffer's sample.
amount
property
writable
Returns the number of lines in Sniffer's sample.
skips
property
writable
Returns the skipped lines excluded from this Sniffer's sample.
lines
property
Returns a list of integer line numbers comprising the sample.
dialect
property
writable
Returns this Sniffer's dialect.
rows
property
Returns list of sample rows from this Sniffer's sample string.
This method splits the sample string on new line chars, strips white spaces and replaces all double-quotes with single quotes.
Returns:
Type | Description |
---|---|
List[List[str]]
|
A list of list of strings from the sample string |
__init__(infile, start=0, amount=100, skips=None, delimiters=[',', ';', '|', '\t'])
Initialize this sniffer.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
infile
|
IO[str]
|
A I/O stream instance such as returned by open. |
required |
start
|
int
|
The start line of infile for collecting a sample of lines. |
0
|
amount
|
int
|
The number of infile lines to sample for dialect detection and locating header and metadata positions. The initial value defaults to the smaller of the infiles length or 100 lines. |
100
|
skips
|
Optional[List[int]]
|
Line numbers to ignore during sample collection. |
None
|
delimiters
|
List[str] | None
|
A restricted list of delimiter strings for improving dialect detection. If None, any character will be considered a valid delimiter. |
[',', ';', '|', '\t']
|
Raises:
Type | Description |
---|---|
SoptIteration
|
is raised if start is greater than infile's size. |
Notes
Sniffer deviates from Python's Sniffer in that infile is strictly an IO stream, not a list because detecting the metadata and header structures requires movement within the file via 'seek'.
Source code in src/tabbed/sniffing.py
sniff(delimiters=None)
Returns a clevercsv SimpleDialect from this instances sample.
Dialect is detected using clevercsv's sniffer as it has shown improved dialect detection accuracy over Python's csv sniffer built-in.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
delimiters
|
Optional[List[str]]
|
A string of possibly valid delimiters see csv.Sniffer.sniff. |
None
|
Returns:
Type | Description |
---|---|
None
|
A SimpleDialect instance (see clevercsv.dialect) or None if sniffing |
None
|
is inconclusive. |
References
van den Burg, G.J.J., Nazábal, A. & Sutton, C. Wrangling messy CSV files by detecting row and type patterns. Data Min Knowl Disc 33, 1799–1820 (2019). https://doi.org/10.1007/s10618-019-00646-y
Source code in src/tabbed/sniffing.py
types(poll, exclude=['', ' ', '-', 'nan', 'NaN', 'NAN'])
Infer the column types from the last poll count rows.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
poll
|
int
|
The number of last sample rows to poll for type. |
required |
exclude
|
List[str]
|
A sequence of characters that indicate missing values. Rows containing these strings will be ignored for type determination. |
['', ' ', '-', 'nan', 'NaN', 'NAN']
|
Returns:
Type | Description |
---|---|
CellTypes
|
A list of types and a boolean indicating if types are |
bool
|
consistent across polled rows. |
Source code in src/tabbed/sniffing.py
datetime_formats(poll, exclude=['', ' ', '-', 'nan', 'NaN', 'NAN'])
Infer time, date or datetime formats from last poll count rows.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
poll
|
int
|
The number of last sample rows to poll for type and format consistency. |
required |
Returns:
Type | Description |
---|---|
List[str | None]
|
A tuple containing a list of formats the same length as last polled |
bool
|
row and a boolean indicating if the formats are consistent across |
Tuple[List[str | None], bool]
|
the polled rows. Columns that are not time, date or datetime type |
Tuple[List[str | None], bool]
|
have a format of None. |
Source code in src/tabbed/sniffing.py
header(poll, exclude=['', ' ', '-', 'nan', 'NaN', 'NAN'])
Detects the header row (if any) from this Sniffers sample rows.
Headers are located using one of two possible methods. 1. If the last row contains mixed types and the last poll rows have consistent types, then the first row from the last whose types differ from the last row types and whose length matches the last row is taken as the header. 2. If the last poll rows are all string type. The first row from the last with string values that have never been seen in the previous rows and whose length matches the last row is taken to be the header. Caution, the poll amount should be sufficiently large enough to sample the possible string values expected in the data section. If the header is not correct, consider increasing the poll rows parameter.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
poll
|
int
|
The number of last sample rows to poll for locating the header using string or type differences. Poll should be large enough to capture many of the string values that appear in the data section. |
required |
exclude
|
List[str]
|
A sequence of characters that indicate missing values. Rows containing these strings will be ignored. |
['', ' ', '-', 'nan', 'NaN', 'NAN']
|
Notes
If no header is detected this method constructs a header. The names in this header are of the form; 'Column_1', ... 'Column_n' where n is the expected number of columns from the last row of the sample rows. Just like all other file sniffers, this heuristic will make mistakes. A judicious sample choice that ignores problematic rows via the skip parameter may aide detection.
Returns:
Type | Description |
---|---|
Header
|
A Header dataclass instance. |
Source code in src/tabbed/sniffing.py
metadata(header, poll=None, exclude=['', ' ', '-', 'nan', 'NaN', 'NAN'])
Detects the metadata section (if any) in this Sniffer's sample.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
header
|
Header | None
|
A Header dataclass instance. |
required |
poll
|
Optional[int]
|
The number of last sample rows to poll for locating metadata by length differences if the header arg is None. |
None
|
exclude
|
List[str]
|
A sequence of characters that indicate missing values. Rows containing these strings will be ignored during metadata detection. This is ignored if a header is given. |
['', ' ', '-', 'nan', 'NaN', 'NAN']
|
Returns:
Type | Description |
---|---|
MetaData
|
A MetaData dataclass instance. |
Source code in src/tabbed/sniffing.py
tabbed.sniffing.Header
dataclass
An immutable dataclass representation of a text file's header.
Attributes:
Name | Type | Description |
---|---|---|
line |
int | None
|
The integer line number of this Header. If None, the header was not derived from a file. |
names |
List[str]
|
The string names of each of the columns comprising the header. If these names contain spaces or repeat, this representation automatically amends them. |
string |
str | None
|
The original string that was split to create header names. If None, the names were not derived from a file. |
Source code in src/tabbed/sniffing.py
__post_init__()
_amend()
Ensures header names have no spaces and are unique.
Header names may not have spaces. This function replaces spaces with underscores. Header names must be unique. This function adds an underscore plus an integer to names that repeat.
Source code in src/tabbed/sniffing.py
tabbed.sniffing.MetaData
dataclass
An immutable dataclass representing a text file's metadata section.
Attributes:
Name | Type | Description |
---|---|---|
lines |
Tuple[int, int | None]
|
A 2-tuple of start and stop of file lines containing metadata. If None, the file does not contain a metadata section. |
string |
str | None
|
The string of metadata with no conversion read from file instance. If None, the file does not contain a metadata section. |