Skip to content

Commit

Permalink
Merge pull request #208 from ckan/github-206-empty-lines
Browse files Browse the repository at this point in the history
Skip empty lines instead of erroring
  • Loading branch information
ThrawnCA authored Mar 15, 2024
2 parents 95edb97 + 9575e05 commit 8ad1ac9
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 0 deletions.
4 changes: 4 additions & 0 deletions ckanext/xloader/loader.py
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,7 @@ def load_csv(csv_filepath, resource_id, mimetype='text/csv', logger=None):
# Get the list of rows to skip. The rows in the tabulator stream are
# numbered starting with 1.
skip_rows = list(range(1, header_offset + 1))
skip_rows.append({'type': 'preset', 'value': 'blank'})

# Get the delimiter used in the file
delimiter = stream.dialect.get('delimiter')
Expand Down Expand Up @@ -351,12 +352,14 @@ def load_table(table_filepath, resource_id, mimetype='text/csv', logger=None):
try:
file_format = os.path.splitext(table_filepath)[1].strip('.')
with UnknownEncodingStream(table_filepath, file_format, decoding_result,
skip_rows=[{'type': 'preset', 'value': 'blank'}],
post_parse=[TypeConverter().convert_types]) as stream:
header_offset, headers = headers_guess(stream.sample)
except TabulatorException:
try:
file_format = mimetype.lower().split('/')[-1]
with UnknownEncodingStream(table_filepath, file_format, decoding_result,
skip_rows=[{'type': 'preset', 'value': 'blank'}],
post_parse=[TypeConverter().convert_types]) as stream:
header_offset, headers = headers_guess(stream.sample)
except TabulatorException as e:
Expand All @@ -378,6 +381,7 @@ def load_table(table_filepath, resource_id, mimetype='text/csv', logger=None):
# Get the list of rows to skip. The rows in the tabulator stream are
# numbered starting with 1. We also want to skip the header row.
skip_rows = list(range(1, header_offset + 2))
skip_rows.append({'type': 'preset', 'value': 'blank'})

TYPES, TYPE_MAPPING = get_types()
types = type_guess(stream.sample[1:], types=TYPES, strict=True)
Expand Down
10 changes: 10 additions & 0 deletions ckanext/xloader/tests/samples/sample_with_empty_lines.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
date,temperature,place
2011-01-01,1,Galway
2011-01-02,-1,Galway
2011-01-03,0,Galway
2011-01-01,6,Berkeley

,,Berkeley
2011-01-03,5,


12 changes: 12 additions & 0 deletions ckanext/xloader/tests/test_loader.py
Original file line number Diff line number Diff line change
Expand Up @@ -632,6 +632,18 @@ def test_with_blanks(self, Session):
)
assert len(self._get_records(Session, resource_id)) == 3

def test_with_empty_lines(self, Session):
csv_filepath = get_sample_filepath("sample_with_empty_lines.csv")
resource = factories.Resource()
resource_id = resource['id']
loader.load_csv(
csv_filepath,
resource_id=resource_id,
mimetype="text/csv",
logger=logger,
)
assert len(self._get_records(Session, resource_id)) == 6

def test_with_quoted_commas(self, Session):
csv_filepath = get_sample_filepath("sample_with_quoted_commas.csv")
resource = factories.Resource()
Expand Down

0 comments on commit 8ad1ac9

Please sign in to comment.