MCPcopy Index your code
hub / github.com/wireservice/csvkit / parse_column_identifiers

Function parse_column_identifiers

csvkit/cli.py:515–579  ·  view source on GitHub ↗

Parse a comma-separated list of column indices AND/OR names into a list of integer indices. Ranges of integers can be specified with two integers separated by a '-' or ':' character. Ranges of non-integers (e.g. column names) are not supported. Note: Column indices are 1-based.

(ids, column_names, column_offset=1, excluded_columns=None)

Source from the content-addressed store, hash-verified

513
514
515def parse_column_identifiers(ids, column_names, column_offset=1, excluded_columns=None):
516 """
517 Parse a comma-separated list of column indices AND/OR names into a list of integer indices.
518 Ranges of integers can be specified with two integers separated by a '-' or ':' character.
519 Ranges of non-integers (e.g. column names) are not supported.
520 Note: Column indices are 1-based.
521 """
522 if not column_names:
523 return []
524
525 if not ids and not excluded_columns:
526 return range(len(column_names))
527
528 if ids:
529 columns = []
530
531 for c in ids.split(','):
532 try:
533 columns.append(match_column_identifier(column_names, c, column_offset))
534 except ColumnIdentifierError:
535 if ':' in c:
536 a, b = c.split(':', 1)
537 elif '-' in c:
538 a, b = c.split('-', 1)
539 else:
540 raise
541
542 try:
543 a = int(a) if a else 1
544 b = int(b) + 1 if b else len(column_names) + 1
545 except ValueError:
546 raise ColumnIdentifierError(
547 "Invalid range %s. Ranges must be two integers separated by a - or : character.")
548
549 for x in range(a, b):
550 columns.append(match_column_identifier(column_names, x, column_offset))
551 else:
552 columns = range(len(column_names))
553
554 excludes = []
555
556 if excluded_columns:
557 for c in excluded_columns.split(','):
558 try:
559 excludes.append(match_column_identifier(column_names, c, column_offset))
560 except ColumnIdentifierError:
561 if ':' in c:
562 a, b = c.split(':', 1)
563 elif '-' in c:
564 a, b = c.split('-', 1)
565 else:
566 # Ignore unknown columns.
567 continue
568
569 try:
570 a = int(a) if a else 1
571 b = int(b) + 1 if b else len(column_names)
572 except ValueError:

Callers 6

mainMethod · 0.90
mainMethod · 0.90
test_range_notationMethod · 0.90

Calls 2

match_column_identifierFunction · 0.85

Tested by 3

test_range_notationMethod · 0.72