Permalink
Browse files

Fix line counting in csvclean. Closes #180.

  • Loading branch information...
1 parent dbed0b5 commit 9e4b442a0f2047e16a09def2eee81ebd96dddc18 @onyxfish onyxfish committed Aug 16, 2012
Showing with 22 additions and 6 deletions.
  1. +5 −4 csvkit/cleanup.py
  2. +2 −2 csvkit/utilities/csvclean.py
  3. +15 −0 tests/test_utilities/test_csvclean.py
View
@@ -66,7 +66,6 @@ def __init__(self, reader):
self.reader = reader
self.column_names = reader.next()
- self.input_rows = 1
self.errs = []
self.rows_joined = 0
self.joins = 0
@@ -75,13 +74,13 @@ def checked_rows(self):
"""
A generator which yields rows which are ready to write to output.
"""
+ line_number = self.reader.line_num
+
for row in self.reader:
- self.input_rows += 1
- line_number = self.input_rows + 1 # add one for 1-based counting
try:
if len(row) != len(self.column_names):
- raise LengthMismatchError(line_number,row,len(self.column_names))
+ raise LengthMismatchError(line_number, row, len(self.column_names))
yield row
except LengthMismatchError, e:
@@ -111,3 +110,5 @@ def checked_rows(self):
except CSVTestException, e:
self.errs.append(e)
+ line_number = self.reader.line_num
+
@@ -24,7 +24,7 @@ def main(self):
if checker.errs:
for e in checker.errs:
- self.output_file.write('Line %i: %s\n' % (e.line_number,e.msg))
+ self.output_file.write('Line %i: %s\n' % (e.line_number, e.msg))
else:
self.output_file.write('No errors.\n')
@@ -48,10 +48,10 @@ def main(self):
err_header = ['line_number', 'msg']
err_header.extend(checker.column_names)
err_file.writerow(err_header)
+ err_count = len(checker.errs)
for e in checker.errs:
err_file.writerow(self._format_error_row(e))
- err_count = len(checker.errs)
self.output_file.write('%i error%s logged to %s\n' % (err_count,'' if err_count == 1 else 's', err_filename))
else:
@@ -32,3 +32,18 @@ def test_simple(self):
os.remove('examples/bad_err.csv')
os.remove('examples/bad_out.csv')
+ def test_dry_run(self):
+ args = ['-n', 'examples/bad.csv']
+ output_file = StringIO()
+
+ utility = CSVClean(args, output_file)
+ utility.main()
+
+ self.assertFalse(os.path.exists('examples/bad_err.csv'))
+ self.assertFalse(os.path.exists('examples/bad_out.csv'))
+
+ output = StringIO(output_file.getvalue())
+
+ self.assertEqual(output.next()[:6], 'Line 1')
+ self.assertEqual(output.next()[:6], 'Line 2')
+

0 comments on commit 9e4b442

Please sign in to comment.