From fcc557122fbf73b6e45f2ca64ab9e85c5efe2590 Mon Sep 17 00:00:00 2001 From: Andreas Thalhammer Date: Tue, 26 Sep 2023 23:06:22 +0200 Subject: [PATCH 1/5] added 'column numbers everywhere' to csvcut --- csvkit/utilities/csvcut.py | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/csvkit/utilities/csvcut.py b/csvkit/utilities/csvcut.py index 10d42d148..0269da720 100644 --- a/csvkit/utilities/csvcut.py +++ b/csvkit/utilities/csvcut.py @@ -35,6 +35,9 @@ def add_arguments(self): self.argparser.add_argument( '-x', '--delete-empty-rows', dest='delete_empty', action='store_true', help='After cutting, delete rows which are completely empty.') + self.argparser.add_argument( + '-N', '--column-numbers', dest='number_columns_everywhere', action='store_true', + help='Add column numbering everywhere, in header and cells.') def main(self): if self.args.names_only: @@ -47,10 +50,16 @@ def main(self): rows, column_names, column_ids = self.get_rows_and_column_names_and_column_ids(**self.reader_kwargs) output = agate.csv.writer(self.output_file, **self.writer_kwargs) - output.writerow([column_names[column_id] for column_id in column_ids]) + if self.args.number_columns_everywhere: + output.writerow([f'{column_id + 1}~{column_names[column_id]}' for column_id in column_ids]) + else: + output.writerow([column_names[column_id] for column_id in column_ids]) for row in rows: - out_row = [row[column_id] if column_id < len(row) else None for column_id in column_ids] + if self.args.number_columns_everywhere: + out_row = [f'{column_id + 1}~{row[column_id]}' if column_id < len(row) else None for column_id in column_ids] + else: + out_row = [row[column_id] if column_id < len(row) else None for column_id in column_ids] if not self.args.delete_empty or any(out_row): output.writerow(out_row) From 8460d8c4eb701f4818d016677af7466aa26d670d Mon Sep 17 00:00:00 2001 From: Andreas Thalhammer Date: Tue, 26 Sep 2023 23:06:22 +0200 Subject: [PATCH 2/5] added compatibility to --zero --- csvkit/utilities/csvcut.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/csvkit/utilities/csvcut.py b/csvkit/utilities/csvcut.py index 0269da720..7b7952a6e 100644 --- a/csvkit/utilities/csvcut.py +++ b/csvkit/utilities/csvcut.py @@ -50,14 +50,16 @@ def main(self): rows, column_names, column_ids = self.get_rows_and_column_names_and_column_ids(**self.reader_kwargs) output = agate.csv.writer(self.output_file, **self.writer_kwargs) + + one_or_zero = 0 if self.args.zero_based else 1 if self.args.number_columns_everywhere: - output.writerow([f'{column_id + 1}~{column_names[column_id]}' for column_id in column_ids]) + output.writerow([f'{column_id + one_or_zero}~{column_names[column_id]}' for column_id in column_ids]) else: output.writerow([column_names[column_id] for column_id in column_ids]) for row in rows: if self.args.number_columns_everywhere: - out_row = [f'{column_id + 1}~{row[column_id]}' if column_id < len(row) else None for column_id in column_ids] + out_row = [f'{column_id + one_or_zero}~{row[column_id]}' if column_id < len(row) else None for column_id in column_ids] else: out_row = [row[column_id] if column_id < len(row) else None for column_id in column_ids] From c686943974754692aef494d4c5a79f44acc1b356 Mon Sep 17 00:00:00 2001 From: Andreas Thalhammer Date: Sun, 1 Oct 2023 16:28:16 +0200 Subject: [PATCH 3/5] made flake8 compliant --- csvkit/utilities/csvcut.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/csvkit/utilities/csvcut.py b/csvkit/utilities/csvcut.py index 7b7952a6e..f108a7b54 100644 --- a/csvkit/utilities/csvcut.py +++ b/csvkit/utilities/csvcut.py @@ -59,7 +59,8 @@ def main(self): for row in rows: if self.args.number_columns_everywhere: - out_row = [f'{column_id + one_or_zero}~{row[column_id]}' if column_id < len(row) else None for column_id in column_ids] + out_row = [f'{column_id + one_or_zero}~{row[column_id]}' + if column_id < len(row) else None for column_id in column_ids] else: out_row = [row[column_id] if column_id < len(row) else None for column_id in column_ids] From 9530992ee6bb521a8f7f0cd30961ca0a5a566b82 Mon Sep 17 00:00:00 2001 From: Andreas Thalhammer Date: Wed, 4 Oct 2023 08:57:01 +0200 Subject: [PATCH 4/5] adding test for csvcut -N --- tests/test_utilities/test_csvcut.py | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/tests/test_utilities/test_csvcut.py b/tests/test_utilities/test_csvcut.py index 520e66f61..0d1a44181 100644 --- a/tests/test_utilities/test_csvcut.py +++ b/tests/test_utilities/test_csvcut.py @@ -26,6 +26,12 @@ def test_simple(self): ['1', '3'], ]) + def test_linenumbers(self): + self.assertRows(['-c', '1,3', '-N', 'examples/dummy.csv'], [ + ['1~a', '3~c'], + ['1~1', '3~3'], + ]) + def test_linenumbers(self): self.assertRows(['-c', '1,3', '-l', 'examples/dummy.csv'], [ ['line_number', 'a', 'c'], From ac33cd5b5e6e7bc7989494c887b932c6e53136e4 Mon Sep 17 00:00:00 2001 From: Andreas Thalhammer Date: Wed, 4 Oct 2023 10:07:03 +0200 Subject: [PATCH 5/5] fixed ctrl+p/ctrl+v error on function name --- tests/test_utilities/test_csvcut.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/test_utilities/test_csvcut.py b/tests/test_utilities/test_csvcut.py index 563f26322..e250ae0ef 100644 --- a/tests/test_utilities/test_csvcut.py +++ b/tests/test_utilities/test_csvcut.py @@ -24,7 +24,7 @@ def test_simple(self): ['1', '3'], ]) - def test_linenumbers(self): + def test_column_numbers(self): self.assertRows(['-c', '1,3', '-N', 'examples/dummy.csv'], [ ['1~a', '3~c'], ['1~1', '3~3'],