Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
Unreleased
----------

- fix: :doc:`/scripts/csvformat` accepts empty input with :code:`--skip-header` and :code:`--no-header-row`, including when :code:`--skip-lines` consumes the entire input.

- feat: :doc:`/scripts/csvcut` adds an :code:`--ignore-unknown-columns` option to skip identifiers in :code:`-c/--columns` that do not match a column in the input.
- feat: :doc:`/scripts/csvclean` adds a :code:`--remove-empty-columns` option to remove empty columns from standard output.
- feat: :doc:`/scripts/in2csv` guesses the ``ndjson`` format for files with :code:`.ndjson`, :code:`.jsonl` and :code:`.jl` extensions.
Expand Down
6 changes: 4 additions & 2 deletions csvkit/utilities/csvformat.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,12 +91,14 @@ def main(self):
reader = agate.csv.reader(self.skip_lines(), **self.reader_kwargs)
if self.args.no_header_row:
# Peek at a row to get the number of columns.
_row = next(reader)
_row = next(reader, None)
if _row is None:
return
headers = make_default_headers(len(_row))
reader = itertools.chain([headers, _row], reader)

if self.args.skip_header:
next(reader)
next(reader, None)

writer.writerows(reader)

Expand Down
13 changes: 13 additions & 0 deletions tests/test_utilities/test_csvformat.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,19 @@ def test_skip_header(self):
'1,2,3',
])

def test_empty_with_header_options(self):
for options in (['--skip-header'], ['--no-header-row'],
['--no-header-row', '--skip-header']):
with self.subTest(options=options):
with stdin_as_string(io.BytesIO(b'')):
self.assertLines(options, [])

def test_skip_all_lines_with_header_options(self):
for options in (['--skip-header'], ['--no-header-row'],
['--no-header-row', '--skip-header']):
with self.subTest(options=options):
self.assertLines(options + ['--skip-lines', '2', 'examples/dummy.csv'], [])

def test_skip_header_no_header_row(self):
self.assertLines(['--no-header-row', '--skip-header', 'examples/no_header_row.csv'], [
'1,2,3',
Expand Down