Skip to content

Commit

Permalink
Use Table.to_csv
Browse files Browse the repository at this point in the history
  • Loading branch information
James McKinney committed Feb 3, 2016
1 parent 87b0799 commit e455ae6
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 13 deletions.
11 changes: 3 additions & 8 deletions csvkit/convert/__init__.py
Expand Up @@ -13,7 +13,7 @@
SUPPORTED_FORMATS = ['csv', 'dbf', 'geojson', 'json', 'ndjson', 'fixed', 'xls', 'xlsx']


def convert(f, format, schema=None, key=None, **kwargs):
def convert(f, format, schema=None, key=None, output=None, **kwargs):
"""
Convert a file of a specified format to CSV.
"""
Expand All @@ -27,9 +27,9 @@ def convert(f, format, schema=None, key=None, **kwargs):
if not schema:
raise ValueError('schema must not be null when format is "fixed"')

return fixed2csv(f, schema, **kwargs)
output_file.write(fixed2csv(f, schema, **kwargs))
elif format == 'geojson':
return geojson2csv(f, **kwargs)
output_file.write(geojson2csv(f, **kwargs))
elif format in ('csv', 'dbf', 'json', 'ndjson', 'xls', 'xlsx'):
if format == 'csv':
table = agate.Table.from_csv(f, **kwargs)
Expand All @@ -46,12 +46,7 @@ def convert(f, format, schema=None, key=None, **kwargs):
column_names = db.field_names
table = agate.Table(db, column_names)

output = six.StringIO()
table.to_csv(output)
result = output.getvalue()
output.close()

return result
else:
raise ValueError('format "%s" is not supported' % format)

Expand Down
4 changes: 1 addition & 3 deletions csvkit/utilities/in2csv.py
Expand Up @@ -74,9 +74,7 @@ def main(self):
if filetype == 'fixed':
kwargs['output'] = self.output_file

data = convert.convert(self.input_file, filetype, **kwargs)

self.output_file.write(data)
convert.convert(self.input_file, filetype, output=self.output_file, **kwargs)


def launch_new_instance():
Expand Down
8 changes: 6 additions & 2 deletions tests/test_convert/test_convert.py
@@ -1,5 +1,7 @@
#!/usr/bin/env python

import six

try:
import unittest2 as unittest
except ImportError:
Expand All @@ -11,11 +13,13 @@
class TestConvert(unittest.TestCase):

def test_valid_file(self):
output = six.StringIO()

with open('examples/test.xls', 'rb') as f:
output = convert.convert(f, 'xls')
convert.convert(f, 'xls', output=output)

with open('examples/testxls_converted.csv', 'r') as f:
self.assertEquals(f.read(), output)
self.assertEquals(f.read(), output.getvalue())

def test_no_file(self):
self.assertRaises(ValueError, convert.convert, None, 'xls')
Expand Down

0 comments on commit e455ae6

Please sign in to comment.