Skip to content

Commit

Permalink
Merge b7da6df into 178eb76
Browse files Browse the repository at this point in the history
  • Loading branch information
dannysepler committed Apr 5, 2020
2 parents 178eb76 + b7da6df commit 5ed1580
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 6 deletions.
7 changes: 4 additions & 3 deletions csvkit/cli.py
Expand Up @@ -295,12 +295,13 @@ def get_column_types(self):
type_kwargs = {}

text_type = agate.Text(**type_kwargs)
number_type = agate.Number(locale=self.args.locale, **type_kwargs)

if self.args.no_inference:
if getattr(self.args, 'no_inference', None):
types = [text_type]
elif getattr(self.args, 'infer_only_numbers', None):
types = [number_type, text_type]
else:
number_type = agate.Number(locale=self.args.locale, **type_kwargs)

# See the order in the `agate.TypeTester` class.
types = [
agate.Boolean(**type_kwargs),
Expand Down
14 changes: 11 additions & 3 deletions csvkit/utilities/csvformat.py
Expand Up @@ -9,7 +9,7 @@

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

def add_arguments(self):
self.argparser.add_argument('-D', '--out-delimiter', dest='out_delimiter',
Expand Down Expand Up @@ -49,9 +49,17 @@ def main(self):
if self.additional_input_expected():
sys.stderr.write('No input file or piped data provided. Waiting for standard input:\n')

reader = agate.csv.reader(self.skip_lines(), **self.reader_kwargs)
if self.args.out_quoting == 2:
self.args.infer_only_numbers = True
else:
self.args.no_inference = True
self.reader_kwargs['column_types'] = self.get_column_types()

table = agate.Table.from_csv(self.skip_lines(), **self.reader_kwargs)
writer = agate.csv.writer(self.output_file, **self.writer_kwargs)
writer.writerows(reader)
if not self.args.no_header_row:
writer.writerow(table.column_names)
writer.writerows(table.rows)


def launch_new_instance():
Expand Down
11 changes: 11 additions & 0 deletions tests/test_utilities/test_csvformat.py
Expand Up @@ -72,6 +72,17 @@ def test_doublequote(self):

input_file.close()

def test_numeric_inference(self):
input_file = six.StringIO('a,b,c\na,2,3\n')

with stdin_as_string(input_file):
self.assertLines(['-U', '2'], [
'"a","b","c"',
'"a",2,3',
])

input_file.close()

def test_escapechar(self):
input_file = six.StringIO('a,b,c\n1"2,3,4\n')

Expand Down

0 comments on commit 5ed1580

Please sign in to comment.