Skip to content

Commit 177cf15

Browse files
authored
Merge pull request #1281 from lamdevhs/master
feat(csvcut): Make -C ignore unknown columns
2 parents d9ee911 + 13cf6a3 commit 177cf15

File tree

6 files changed

+12
-3
lines changed

6 files changed

+12
-3
lines changed

CHANGELOG.rst

+1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ Unreleased
77
- feat: :doc:`/scripts/csvsql` adds a :code:`--sql-delimiter` option, to set a different delimiter than ``;`` for the :code:`--query`, :code:`--before-insert` and :code:`after-insert` options.
88
- feat: :doc:`/scripts/sql2csv` adds a :code:`--execution-option` option.
99
- feat: :doc:`/scripts/sql2csv` uses the ``stream_results=True`` execution option, by default, to not load all data into memory at once.
10+
- feat: Make :code:`csvcut -C` ignore unknown columns instead of throwing an exception.
1011
- fix: :doc:`/scripts/csvsql` uses a default value of 1 for the :code:`--min-col-len` and :code:`--col-len-multiplier` options.
1112
- fix: The :code:`--encoding` option defaults to the ``PYTHONIOENCODING`` environment variable if set.
1213
- fix: For type inference, number takes priority over date-time, if not using the :code:`--datetime-format` option.

csvkit/cli.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -561,7 +561,8 @@ def parse_column_identifiers(ids, column_names, column_offset=1, excluded_column
561561
elif '-' in c:
562562
a, b = c.split('-', 1)
563563
else:
564-
raise
564+
# ignore unknown columns
565+
continue
565566

566567
try:
567568
a = int(a) if a else 1

docs/scripts/csvcut.rst

+1-1
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ Filters and truncates CSV files. Like the Unix "cut" command, but for tabular da
3333
-C NOT_COLUMNS, --not-columns NOT_COLUMNS
3434
A comma-separated list of column indices, names or
3535
ranges to be excluded, e.g. "1,id,3-5". Defaults to no
36-
columns.
36+
columns. Ignores unknown columns.
3737
-x, --delete-empty-rows
3838
After cutting, delete rows which are completely empty.
3939

man/csvcut.1

+1-1
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ optional arguments:
6262
\-C NOT_COLUMNS, \-\-not\-columns NOT_COLUMNS
6363
A comma\-separated list of column indices, names or
6464
ranges to be excluded, e.g. \(dq1,id,3\-5\(dq. Defaults to no
65-
columns.
65+
columns. Ignores unknown columns.
6666
\-x, \-\-delete\-empty\-rows
6767
After cutting, delete rows which are completely empty.
6868
.ft P

tests/test_cli.py

+1
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ def test_match_column_which_could_be_integer_name_is_treated_as_positional_id(se
2323
def test_parse_column_identifiers(self):
2424
self.assertEqual([2, 0, 1], parse_column_identifiers('i_work_here,1,name', self.headers))
2525
self.assertEqual([2, 1, 1], parse_column_identifiers('i_work_here,1,name', self.headers, column_offset=0))
26+
self.assertEqual([1, 1], parse_column_identifiers('i_work_here,1,name', self.headers, column_offset=0, excluded_columns="i_work_here,foobar"))
2627

2728
def test_range_notation(self):
2829
self.assertEqual([0, 1, 2], parse_column_identifiers('1:3', self.headers))

tests/test_utilities/test_csvcut.py

+6
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,12 @@ def test_exclude(self):
5555
['2'],
5656
])
5757

58+
def test_exclude_unknown_columns(self):
59+
self.assertRows(['-C', '1,foo,42', 'examples/dummy.csv'], [
60+
['b', 'c'],
61+
['2', '3'],
62+
])
63+
5864
def test_include_and_exclude(self):
5965
self.assertRows(['-c', '1,3', '-C', '3', 'examples/dummy.csv'], [
6066
['a'],

0 commit comments

Comments
 (0)