Skip to content

Commit

Permalink
Fixes #226 - Allow importing ragged CSV files (#456)
Browse files Browse the repository at this point in the history
  • Loading branch information
claudep authored Feb 12, 2020
1 parent 7a6c623 commit aaeb5c8
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 0 deletions.
5 changes: 5 additions & 0 deletions HISTORY.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,11 @@
will remain in Tablib 1.x and will be fixed (reversed) in Tablib 2.0.0 (#453). If you
count on the broken behavior, please update your code when you upgrade to Tablib 2.x.

### Improvements

- Tablib is now able to import CSV content where not all rows have the same
length. Missing columns on any line receive the empty string (#226).

## 1.0.0 (2020-01-13)

### Breaking changes
Expand Down
2 changes: 2 additions & 0 deletions src/tablib/formats/_csv.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ def import_set(cls, dset, in_stream, headers=True, **kwargs):
if (i == 0) and (headers):
dset.headers = row
elif row:
if i > 0 and len(row) < dset.width:
row += [''] * (dset.width - len(row))
dset.append(row)

@classmethod
Expand Down
19 changes: 19 additions & 0 deletions tests/test_tablib.py
Original file line number Diff line number Diff line change
Expand Up @@ -804,6 +804,25 @@ def test_csv_import_set_with_unicode_str(self):
data.csv = csv_text
self.assertEqual(data.width, 7)

def test_csv_import_set_ragged(self):
"""Import CSV set when not all rows have the same length."""
csv_text = (
"H1,H2,H3\n"
"A,B\n"
"C,D,E\n"
"\n"
"F\n"
)
dataset = tablib.import_set(csv_text, format="csv")
self.assertEqual(
str(dataset),
'H1|H2|H3\n'
'--|--|--\n'
'A |B | \n'
'C |D |E \n'
'F | | '
)

def test_csv_export(self):
"""Verify exporting dataset object as CSV."""

Expand Down

0 comments on commit aaeb5c8

Please sign in to comment.