diff --git a/csvkit/utilities/csvlook.py b/csvkit/utilities/csvlook.py index 819af87b1..2c59ee51f 100644 --- a/csvkit/utilities/csvlook.py +++ b/csvkit/utilities/csvlook.py @@ -36,40 +36,7 @@ def main(self): column_names.insert(0, 'line_number') rows = [list(itertools.chain([str(i + 1)], row)) for i, row in enumerate(rows)] - # Convert to normal list of rows - rows = list(rows) - - # Insert the column names at the top - rows.insert(0, column_names) - - widths = [] - - for row in rows: - for i, v in enumerate(row): - try: - if len(v) > widths[i]: - widths[i] = len(v) - except IndexError: - widths.append(len(v)) - - # Dashes span each width with '+' character at intersection of - # horizontal and vertical dividers. - divider = '|--' + '-+-'.join('-' * w for w in widths) + '--|' - - self.output_file.write('%s\n' % divider) - - for i, row in enumerate(rows): - output = [] - - for j, d in enumerate(row): - if d is None: - d = '' - output.append(' %s ' % six.text_type(d).ljust(widths[j])) - - self.output_file.write('| %s |\n' % ('|'.join(output))) - - if (i == 0 or i == len(rows) - 1): - self.output_file.write('%s\n' % divider) + agate.Table(list(rows)).print_table(output=self.output_file) def launch_new_instance(): diff --git a/examples/test_utf8.csv b/examples/test_utf8.csv index 45049640d..5022f51d4 100644 --- a/examples/test_utf8.csv +++ b/examples/test_utf8.csv @@ -1,3 +1,3 @@ -a,b,c +foo,bar,baz 1,2,3 4,5,ʤ \ No newline at end of file diff --git a/tests/test_utilities/test_csvcut.py b/tests/test_utilities/test_csvcut.py index f715c17d4..14bdfde7e 100644 --- a/tests/test_utilities/test_csvcut.py +++ b/tests/test_utilities/test_csvcut.py @@ -46,7 +46,7 @@ def test_unicode(self): input_file = six.StringIO(output_file.getvalue()) reader = agate.reader(input_file) - self.assertEqual(next(reader), ['a', 'c']) + self.assertEqual(next(reader), ['foo', 'baz']) self.assertEqual(next(reader), ['1', '3']) self.assertEqual(next(reader), ['4', u'ʤ']) diff --git a/tests/test_utilities/test_csvlook.py b/tests/test_utilities/test_csvlook.py index bc882503b..c1c1b3f53 100644 --- a/tests/test_utilities/test_csvlook.py +++ b/tests/test_utilities/test_csvlook.py @@ -30,12 +30,12 @@ def test_simple(self): input_file = six.StringIO(output_file.getvalue()) - self.assertEqual(next(input_file), '|----+---+----|\n') - self.assertEqual(next(input_file), '| a | b | c |\n') - self.assertEqual(next(input_file), '|----+---+----|\n') - self.assertEqual(next(input_file), '| 1 | 2 | 3 |\n') - self.assertEqual(next(input_file), '| 1 | 4 | 5 |\n') - self.assertEqual(next(input_file), '|----+---+----|\n') + self.assertEqual(next(input_file), '|-------+---+----|\n') + self.assertEqual(next(input_file), '| A | B | C |\n') + self.assertEqual(next(input_file), '|-------+---+----|\n') + self.assertEqual(next(input_file), '| True | 2 | 3 |\n') + self.assertEqual(next(input_file), '| True | 4 | 5 |\n') + self.assertEqual(next(input_file), '|-------+---+----|\n') def test_no_header(self): args = ['--no-header-row', 'examples/no_header_row3.csv'] @@ -46,12 +46,12 @@ def test_no_header(self): input_file = six.StringIO(output_file.getvalue()) - self.assertEqual(next(input_file), '|----------+---------+----------|\n') - self.assertEqual(next(input_file), '| column1 | column2 | column3 |\n') - self.assertEqual(next(input_file), '|----------+---------+----------|\n') - self.assertEqual(next(input_file), '| 1 | 2 | 3 |\n') - self.assertEqual(next(input_file), '| 4 | 5 | 6 |\n') - self.assertEqual(next(input_file), '|----------+---------+----------|\n') + self.assertEqual(next(input_file), '|----+---+----|\n') + self.assertEqual(next(input_file), '| A | B | C |\n') + self.assertEqual(next(input_file), '|----+---+----|\n') + self.assertEqual(next(input_file), '| 1 | 2 | 3 |\n') + self.assertEqual(next(input_file), '| 4 | 5 | 6 |\n') + self.assertEqual(next(input_file), '|----+---+----|\n') def test_unicode(self): args = ['examples/test_utf8.csv'] @@ -64,8 +64,24 @@ def test_unicode(self): input_file = six.StringIO(output_file.getvalue()) self.assertEqual(next(input_file), '|----+---+----|\n') - self.assertEqual(next(input_file), '| a | b | c |\n') + self.assertEqual(next(input_file), '| A | B | C |\n') self.assertEqual(next(input_file), '|----+---+----|\n') self.assertEqual(next(input_file), '| 1 | 2 | 3 |\n') self.assertEqual(next(input_file), u'| 4 | 5 | ʤ |\n') self.assertEqual(next(input_file), '|----+---+----|\n') + + def test_linenumbers(self): + args = ['--linenumbers', 'examples/dummy3.csv'] + output_file = six.StringIO() + utility = CSVLook(args, output_file) + + utility.main() + + input_file = six.StringIO(output_file.getvalue()) + + self.assertEqual(next(input_file), '|----+------+---+----|\n') + self.assertEqual(next(input_file), '| A | B | C | D |\n') + self.assertEqual(next(input_file), '|----+------+---+----|\n') + self.assertEqual(next(input_file), '| 1 | True | 2 | 3 |\n') + self.assertEqual(next(input_file), '| 2 | True | 4 | 5 |\n') + self.assertEqual(next(input_file), '|----+------+---+----|\n')