Skip to content

Commit

Permalink
csvlook: Add a --max-precision option, closes #941
Browse files Browse the repository at this point in the history
  • Loading branch information
jpmckinney committed Oct 18, 2023
1 parent 398024c commit 897e3df
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 3 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ Unreleased
----------

* :doc:`/scripts/csvformat` adds a :code:`--skip-header` (:code:`-E`) option to not output a header row.
* :doc:`/scripts/csvlook` adds a :code:`--max-precision` option to set the maximum number of decimal places to display.
* :doc:`/scripts/csvstat` supports the :code:`--no-inference` (:code:`-I`), :code:`--locale` (:code:`-L`), :code:`--blanks`, :code:`--date-format` and :code:`datetime-format` options.
* :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.
Expand Down
8 changes: 8 additions & 0 deletions csvkit/utilities/csvlook.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#!/usr/bin/env python

import agate
from agate import config

from csvkit.cli import CSVKitUtility

Expand All @@ -18,6 +19,9 @@ def add_arguments(self):
self.argparser.add_argument(
'--max-column-width', dest='max_column_width', type=int,
help='Truncate all columns to at most this width. The remainder will be replaced with ellipsis.')
self.argparser.add_argument(
'--max-precision', dest='max_precision', type=int,
help='The maximum number of decimal places to display. The remainder will be replaced with ellipsis.')
self.argparser.add_argument(
'-y', '--snifflimit', dest='sniff_limit', type=int, default=1024,
help='Limit CSV dialect sniffing to the specified number of bytes. '
Expand All @@ -30,6 +34,9 @@ def main(self):
if self.additional_input_expected():
self.argparser.error('You must provide an input file or piped data.')

if self.args.max_precision is not None:
agate.set_option('decimal_truncation_chars', '')

sniff_limit = self.args.sniff_limit if self.args.sniff_limit != -1 else None
table = agate.Table.from_csv(
self.input_file,
Expand All @@ -45,6 +52,7 @@ def main(self):
max_rows=self.args.max_rows,
max_columns=self.args.max_columns,
max_column_width=self.args.max_column_width,
max_precision=self.args.max_precision,
)


Expand Down
10 changes: 7 additions & 3 deletions docs/scripts/csvlook.rst
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,15 @@ Renders a CSV to the command line in a Markdown-compatible, fixed-width format:
--max-column-width MAX_COLUMN_WIDTH
Truncate all columns to at most this width. The
remainder will be replaced with ellipsis.
--max-precision MAX_PRECISION
The maximum number of decimal places to display. The
remainder will be replaced with ellipsis.
-y SNIFF_LIMIT, --snifflimit SNIFF_LIMIT
Limit CSV dialect sniffing to the specified number of
bytes. Specify "0" to disable sniffing.
-I, --no-inference Disable type inference when parsing the input.
Disable reformatting of values.
bytes. Specify "0" to disable sniffing entirely, or
"-1" to sniff the entire file.
-I, --no-inference Disable type inference when parsing the input. Disable
reformatting of values.
If a table is too wide to display properly try piping the output to ``less -S`` or truncating it using :doc:`csvcut`.

Expand Down
7 changes: 7 additions & 0 deletions tests/test_utilities/test_csvlook.py
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,13 @@ def test_max_column_width(self):
'| Tr... | 2 | 3 |',
])

def test_max_precision(self):
self.assertLines(['--max-precision', '0', 'examples/date_like_number.csv'], [
'| a |',
'| - |',
'| 4 |',
])

def test_stdin(self):
input_file = StringIO('a,b,c\n1,2,3\n4,5,6\n')

Expand Down

0 comments on commit 897e3df

Please sign in to comment.