From 17f8dac55822827188b4420abe7346c4aa07086b Mon Sep 17 00:00:00 2001 From: Tabea Kischka Date: Mon, 20 Apr 2015 16:03:52 +0200 Subject: [PATCH 1/2] Included the option --cutoff for csvlook, which allows to specify the maximum number of characters to be printed for each column. I.e. a value of 10 tells csvlook to only print the first 10 characters of each column. --- csvkit/utilities/csvlook.py | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/csvkit/utilities/csvlook.py b/csvkit/utilities/csvlook.py index c4dc04de9..195dac9f6 100644 --- a/csvkit/utilities/csvlook.py +++ b/csvkit/utilities/csvlook.py @@ -12,7 +12,8 @@ class CSVLook(CSVKitUtility): description = 'Render a CSV file in the console as a fixed-width table.' def add_arguments(self): - pass + self.argparser.add_argument('--cutoff', type=int, dest='cutoff', + help='Cut off all columns after this number of characters. Intended to improve readability for CSV files with very long text in columns.') def main(self): rows = CSVKitReader(self.input_file, **self.reader_kwargs) @@ -48,9 +49,15 @@ def main(self): for i, v in enumerate(row): try: if len(v) > widths[i]: - widths[i] = len(v) + if self.args.cutoff: + widths[i] = min(len(v), self.args.cutoff) + else: + widths[i] = len(v) except IndexError: - widths.append(len(v)) + if self.args.cutoff: + widths.append(min(len(v), self.args.cutoff)) + else: + widths.append(len(v)) # Dashes span each width with '+' character at intersection of # horizontal and vertical dividers. @@ -64,6 +71,8 @@ def main(self): for j, d in enumerate(row): if d is None: d = '' + if self.args.cutoff: + d = d[0:self.args.cutoff] output.append(' %s ' % six.text_type(d).ljust(widths[j])) self.output_file.write('| %s |\n' % ('|'.join(output))) From 5e7c92fd6fa9181b13b03c032a7c07face662c7d Mon Sep 17 00:00:00 2001 From: Tabea Kischka Date: Mon, 12 Oct 2015 13:39:41 +0200 Subject: [PATCH 2/2] added "-c" shorthand for cutoff parameter --- csvkit/utilities/csvlook.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/csvkit/utilities/csvlook.py b/csvkit/utilities/csvlook.py index 195dac9f6..e6fb0259f 100644 --- a/csvkit/utilities/csvlook.py +++ b/csvkit/utilities/csvlook.py @@ -12,7 +12,7 @@ class CSVLook(CSVKitUtility): description = 'Render a CSV file in the console as a fixed-width table.' def add_arguments(self): - self.argparser.add_argument('--cutoff', type=int, dest='cutoff', + self.argparser.add_argument('-c', '--cutoff', type=int, dest='cutoff', help='Cut off all columns after this number of characters. Intended to improve readability for CSV files with very long text in columns.') def main(self):