From 23f8abfd4cb5538e3ce0415bb8c1f7269dc1e31c Mon Sep 17 00:00:00 2001 From: James McKinney <26463+jpmckinney@users.noreply.github.com> Date: Thu, 15 Feb 2024 12:06:01 -0500 Subject: [PATCH] feat(csvformat): Add a --out-asv option, closes #1232 --- CHANGELOG.rst | 5 +++++ csvkit/utilities/csvformat.py | 15 +++++++++++++-- 2 files changed, 18 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.rst b/CHANGELOG.rst index 0455627b4..1e304e96e 100644 --- a/CHANGELOG.rst +++ b/CHANGELOG.rst @@ -1,3 +1,8 @@ +Unreleased +---------- + +* feat: :doc:`/scripts/csvformat` adds a :code:`--out-asv` (:code:`--A`) option to use the ASCII unit separator and record separator. + 1.4.0 - February 13, 2024 ------------------------- diff --git a/csvkit/utilities/csvformat.py b/csvkit/utilities/csvformat.py index 74b2c0332..655f5c44b 100644 --- a/csvkit/utilities/csvformat.py +++ b/csvkit/utilities/csvformat.py @@ -21,6 +21,10 @@ def add_arguments(self): self.argparser.add_argument( '-T', '--out-tabs', dest='out_tabs', action='store_true', help='Specify that the output file is delimited with tabs. Overrides "-D".') + self.argparser.add_argument( + '-A', '--out-asv', dest='out_asv', action='store_true', + help='Specify that the output file is delimited with the ASCII unit separator and record separator. ' + 'Overrides "-T", "-D" and "-M".') self.argparser.add_argument( '-Q', '--out-quotechar', dest='out_quotechar', help='Character used to quote strings in the output file.') @@ -45,12 +49,19 @@ def _extract_csv_writer_kwargs(self): if self.args.line_numbers: kwargs['line_numbers'] = True - if self.args.out_tabs: + if self.args.out_asv: + kwargs['delimiter'] = '\x1f' + elif self.args.out_tabs: kwargs['delimiter'] = '\t' elif self.args.out_delimiter: kwargs['delimiter'] = self.args.out_delimiter - for arg in ('quotechar', 'quoting', 'doublequote', 'escapechar', 'lineterminator'): + if self.args.out_asv: + kwargs['lineterminator'] = '\x1e' + elif self.args.out_lineterminator: + kwargs['lineterminator'] = self.args.out_lineterminator + + for arg in ('quotechar', 'quoting', 'doublequote', 'escapechar'): value = getattr(self.args, f'out_{arg}') if value is not None: kwargs[arg] = value