Skip to content

Commit

Permalink
Merge pull request #808 from wireservice/csvformat
Browse files Browse the repository at this point in the history
csvformat supports --no-header-row, --linenumbers, --zero
  • Loading branch information
jpmckinney committed Mar 11, 2018
2 parents 36248b0 + fa160b0 commit 7e8c1ab
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 4 deletions.
3 changes: 2 additions & 1 deletion CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,9 @@ Improvements:
* Add a :code:`--datetime-format` option to set a strptime datetime format string.
* Make :code:`--blanks` a common argument across all tools.
* :code:`-I` is the short option for :code:`--no-inference`.
* :doc:`/scripts/csvclean`, :doc:`/scripts/csvjson`, :doc:`/scripts/csvpy` support :code:`--no-header-row`.
* :doc:`/scripts/csvclean`, :doc:`/scripts/csvformat`, :doc:`/scripts/csvjson`, :doc:`/scripts/csvpy` support :code:`--no-header-row`.
* :doc:`/scripts/csvclean` is faster and no longer requires exponential time in the worst case.
* :doc:`/scripts/csvformat` supports :code:`--linenumbers` and `--zero` (no-op).
* :doc:`/scripts/csvjoin` supports :code:`--snifflimit` and :code:`--no-inference`.
* :doc:`/scripts/csvpy` supports :code:`--linenumbers` (no-op) and :code:`--zero` (no-op).
* :doc:`/scripts/csvsql` adds a :code:`--prefix` option to add expressions like OR IGNORE or OR REPLACE following the INSERT keyword.
Expand Down
5 changes: 4 additions & 1 deletion csvkit/utilities/csvformat.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

class CSVFormat(CSVKitUtility):
description = 'Convert a CSV file to a custom output format.'
override_flags = ['l', 'zero', 'H', 'L', 'date-format', 'datetime-format']
override_flags = ['L', 'blanks', 'date-format', 'datetime-format']

def add_arguments(self):
self.argparser.add_argument('-D', '--out-delimiter', dest='out_delimiter',
Expand All @@ -30,6 +30,9 @@ def add_arguments(self):
def _extract_csv_writer_kwargs(self):
kwargs = {}

if self.args.line_numbers:
kwargs['line_numbers'] = True

if self.args.out_tabs:
kwargs['delimiter'] = '\t'
elif self.args.out_delimiter:
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from setuptools import setup

install_requires = [
'agate>=1.6.0',
'agate>=1.6.1',
'agate-excel>=0.2.2',
'agate-dbf>=0.2.0',
'agate-sql>=0.5.3',
Expand Down
11 changes: 11 additions & 0 deletions tests/test_utilities/test_csvformat.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,17 @@ def test_skip_lines(self):
'1|2|3',
])

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

def test_linenumbers(self):
self.assertLines(['--linenumbers', 'examples/dummy.csv'], [
'line_number,a,b,c',
'1,1,2,3',
])

def test_delimiter(self):
self.assertLines(['-D', '|', 'examples/dummy.csv'], [
'a|b|c',
Expand Down
2 changes: 1 addition & 1 deletion tests/test_utilities/test_csvjson.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ def test_indentation(self):

def test_keying(self):
js = json.loads(self.get_output(['-k', 'a', 'examples/dummy.csv']))
self.assertDictEqual(js, {'true': {'a': True, 'c': 3.0, 'b': 2.0}})
self.assertDictEqual(js, {'True': {'a': True, 'c': 3.0, 'b': 2.0}})

def test_duplicate_keys(self):
output_file = six.StringIO()
Expand Down

0 comments on commit 7e8c1ab

Please sign in to comment.