Skip to content

Commit 78c5bb2

Browse files
committed
feat(csvpy): Add --no-number-ellipsis, --sniff-limit, --no-inference. fix(csvpy): Support --locale, --blanks, --null-value, --date-format, --datetime-format, --skip-lines. Remove --linenumbers, --zero. (closes #1231)
1 parent c42d0db commit 78c5bb2

File tree

3 files changed

+58
-5
lines changed

3 files changed

+58
-5
lines changed

CHANGELOG.rst

+16
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,24 @@
11
Unreleased
22
----------
33

4+
* feat: :doc:`/scripts/csvpy` adds the options:
5+
6+
* :code:`--no-number-ellipsis`, to disable the ellipsis (````) if max precision is exceeded, for example, when using ``table.print_table()``
7+
* :code:`--sniff-limit``
8+
* :code:`--no-inference``
9+
10+
* feat: :doc:`/scripts/csvpy` removes the ``--linenumbers`` and ``--zero`` output options, which had no effect.
411
* feat: :doc:`/scripts/in2csv` adds a :code:`--reset-dimensions` option to `recalculate <https://openpyxl.readthedocs.io/en/stable/optimized.html#worksheet-dimensions>`_ the dimensions of an XLSX file, instead of trusting the file's metadata. csvkit's dependency `agate-excel <https://agate-excel.readthedocs.io/en/latest/>`_ 0.4.0 automatically recalculates the dimensions if the file's metadata expresses dimensions of "A1:A1" (a single cell).
512
* fix: :doc:`/scripts/csvlook` only reads up to :code:`--max-rows` rows instead of the entire file.
13+
* fix: :doc:`/scripts/csvpy` supports the existing input options:
14+
15+
* :code:`--locale`
16+
* :code:`--blanks`
17+
* :code:`--null-value`
18+
* :code:`--date-format`
19+
* :code:`--datetime-format`
20+
* :code:`--skip-lines`
21+
622
* fix: :doc:`/scripts/in2csv`: :code:`--write-sheets` no longer errors when standard input is an XLS or XLSX file.
723
* Update minimum agate version to 1.6.3.
824

csvkit/utilities/csvpy.py

+34-5
Original file line numberDiff line numberDiff line change
@@ -3,40 +3,69 @@
33
import sys
44

55
import agate
6+
from agate import config
67

78
from csvkit.cli import CSVKitUtility
89

910

1011
class CSVPy(CSVKitUtility):
1112
description = 'Load a CSV file into a CSV reader and then drop into a Python shell.'
13+
override_flags = ['l', 'zero']
1214

1315
def add_arguments(self):
14-
self.argparser.add_argument('--dict', dest='as_dict', action='store_true',
15-
help='Load the CSV file into a DictReader.')
16-
self.argparser.add_argument('--agate', dest='as_agate', action='store_true',
17-
help='Load the CSV file into an agate table.')
16+
self.argparser.add_argument(
17+
'--dict', dest='as_dict', action='store_true',
18+
help='Load the CSV file into a DictReader.')
19+
self.argparser.add_argument(
20+
'--agate', dest='as_agate', action='store_true',
21+
help='Load the CSV file into an agate table.')
22+
self.argparser.add_argument(
23+
'--no-number-ellipsis', dest='no_number_ellipsis', action='store_true',
24+
help='Disable the ellipsis if the max precision is exceeded.')
25+
self.argparser.add_argument(
26+
'-y', '--snifflimit', dest='sniff_limit', type=int, default=1024,
27+
help='Limit CSV dialect sniffing to the specified number of bytes. '
28+
'Specify "0" to disable sniffing entirely, or "-1" to sniff the entire file.')
29+
self.argparser.add_argument(
30+
'-I', '--no-inference', dest='no_inference', action='store_true',
31+
help='Disable type inference when parsing the input. This disables the reformatting of values.')
1832

1933
def main(self):
2034
if self.input_file == sys.stdin:
2135
self.argparser.error('csvpy cannot accept input as piped data via STDIN.')
2236

37+
if self.args.no_number_ellipsis:
38+
config.set_option('number_truncation_chars', '')
39+
2340
# Attempt reading filename, will cause lazy loader to access file and raise error if it does not exist
2441
filename = self.input_file.name
2542

2643
if self.args.as_dict:
2744
klass = agate.csv.DictReader
2845
class_name = 'agate.csv.DictReader'
2946
variable_name = 'reader'
47+
input_file = self.skip_lines()
48+
kwargs = {}
3049
elif self.args.as_agate:
3150
klass = agate.Table.from_csv
3251
class_name = 'agate.Table'
3352
variable_name = 'table'
53+
input_file = self.input_file
54+
55+
sniff_limit = self.args.sniff_limit if self.args.sniff_limit != -1 else None
56+
kwargs = dict(
57+
skip_lines=self.args.skip_lines,
58+
sniff_limit=sniff_limit,
59+
column_types=self.get_column_types(),
60+
)
3461
else:
3562
klass = agate.csv.reader
3663
class_name = 'agate.csv.reader'
3764
variable_name = 'reader'
65+
input_file = self.skip_lines()
66+
kwargs = {}
3867

39-
variable = klass(self.input_file, **self.reader_kwargs)
68+
variable = klass(input_file, **kwargs, **self.reader_kwargs)
4069

4170
welcome_message = 'Welcome! "{}" has been loaded in an {} object named "{}".'.format(
4271
filename, class_name, variable_name)

docs/scripts/csvpy.rst

+8
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ Loads a CSV file into a :class:`agate.csv.Reader` object and then drops into a P
1414
[-S] [--blanks] [--null-value NULL_VALUES [NULL_VALUES ...]]
1515
[--date-format DATE_FORMAT] [--datetime-format DATETIME_FORMAT]
1616
[-H] [-K SKIP_LINES] [-v] [-l] [--zero] [-V] [--dict] [--agate]
17+
[--no-number-ellipsis] [-y SNIFF_LIMIT] [-I]
1718
[FILE]
1819
1920
Load a CSV file into a CSV reader and then drop into a Python shell.
@@ -26,6 +27,13 @@ Loads a CSV file into a :class:`agate.csv.Reader` object and then drops into a P
2627
-h, --help show this help message and exit
2728
--dict Load the CSV file into a DictReader.
2829
--agate Load the CSV file into an agate table.
30+
--no-number-ellipsis Disable the ellipsis if the max precision is exceeded.
31+
-y SNIFF_LIMIT, --snifflimit SNIFF_LIMIT
32+
Limit CSV dialect sniffing to the specified number of
33+
bytes. Specify "0" to disable sniffing entirely, or
34+
"-1" to sniff the entire file.
35+
-I, --no-inference Disable type inference when parsing the input. This
36+
disables the reformatting of values.
2937
3038
This tool will automatically use the IPython shell if it is installed, otherwise it will use the running Python shell.
3139

0 commit comments

Comments
 (0)