From f91faa405231ff79ea48531b297b190a67b0bf1b Mon Sep 17 00:00:00 2001 From: James McKinney <26463+jpmckinney@users.noreply.github.com> Date: Tue, 17 Oct 2023 16:55:31 -0400 Subject: [PATCH] csvformat: Add a --skip-header option, closes #929 --- CHANGELOG.rst | 1 + csvkit/utilities/csvformat.py | 9 +++++++-- docs/scripts/csvformat.rst | 1 + tests/test_utilities/test_csvformat.py | 10 ++++++++++ 4 files changed, 19 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.rst b/CHANGELOG.rst index 472244090..520baf90a 100644 --- a/CHANGELOG.rst +++ b/CHANGELOG.rst @@ -1,6 +1,7 @@ Unreleased ---------- +* :doc:`/scripts/csvformat` adds a :code:`--skip-header` option to not output a header row. * :doc:`/scripts/csvstat` adds a :code:`--json` option to output results as JSON text. * :doc:`/scripts/csvstat` adds an :code:`--indent` option to indent the JSON text when :code:`--json` is set. * :doc:`/scripts/csvstat` reports a "Non-null values" statistic (or a :code:`nonnulls` column when :code:`--csv` is set). diff --git a/csvkit/utilities/csvformat.py b/csvkit/utilities/csvformat.py index ba8306556..664462ff2 100644 --- a/csvkit/utilities/csvformat.py +++ b/csvkit/utilities/csvformat.py @@ -12,6 +12,9 @@ class CSVFormat(CSVKitUtility): override_flags = ['L', 'blanks', 'date-format', 'datetime-format'] def add_arguments(self): + self.argparser.add_argument( + '--skip-header', dest='skip_header', action='store_true', + help='Do not output a header row.') self.argparser.add_argument( '-D', '--out-delimiter', dest='out_delimiter', help='Delimiting character of the output CSV file.') @@ -63,9 +66,11 @@ def main(self): if self.args.no_header_row: # Peek at a row to get the number of columns. _row = next(reader) - reader = itertools.chain([_row], reader) headers = make_default_headers(len(_row)) - writer.writerow(headers) + reader = itertools.chain([headers, _row], reader) + + if self.args.skip_header: + next(reader) writer.writerows(reader) diff --git a/docs/scripts/csvformat.rst b/docs/scripts/csvformat.rst index c530f0717..4d3137a1d 100644 --- a/docs/scripts/csvformat.rst +++ b/docs/scripts/csvformat.rst @@ -24,6 +24,7 @@ Convert a CSV file to a custom output format.: optional arguments: -h, --help show this help message and exit + --skip-header Do not output a header row. -D OUT_DELIMITER, --out-delimiter OUT_DELIMITER Delimiting character of the output CSV file. -T, --out-tabs Specify that the output CSV file is delimited with diff --git a/tests/test_utilities/test_csvformat.py b/tests/test_utilities/test_csvformat.py index fc52df230..70567152b 100644 --- a/tests/test_utilities/test_csvformat.py +++ b/tests/test_utilities/test_csvformat.py @@ -19,6 +19,16 @@ def test_skip_lines(self): '1|2|3', ]) + def test_skip_header(self): + self.assertLines(['--skip-header', 'examples/dummy.csv'], [ + '1,2,3', + ]) + + def test_skip_header_no_header_row(self): + self.assertLines(['--no-header-row', '--skip-header', 'examples/no_header_row.csv'], [ + '1,2,3', + ]) + def test_no_header_row(self): self.assertLines(['--no-header-row', 'examples/no_header_row.csv'], [ 'a,b,c',