Skip to content

Commit

Permalink
Merge pull request ckan#208 from ckan/github-206-empty-lines
Browse files Browse the repository at this point in the history
Skip empty lines instead of erroring
# Conflicts:
#	ckanext/xloader/loader.py
### RESOLVED.
  • Loading branch information
ThrawnCA authored and JVickery-TBS committed May 8, 2024
1 parent e82890b commit d5078cd
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 2 deletions.
10 changes: 8 additions & 2 deletions ckanext/xloader/loader.py
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,7 @@ def load_csv(csv_filepath, resource_id, mimetype='text/csv', dialect=None, encod
# 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 @@ -426,17 +427,21 @@ def load_table(table_filepath, resource_id, mimetype='text/csv', dialect=None, e
try:
file_format = os.path.splitext(table_filepath)[1].strip('.')
with UnknownEncodingStream(table_filepath, file_format, decoding_result,
post_parse=[TypeConverter().convert_types], dialect=dialect,
dialect=dialect,
force_encoding=bool(encoding),
skip_rows=[{'type': 'preset', 'value': 'blank'}],
post_parse=[TypeConverter().convert_types],
logger=(logger if not has_logged_dialect else None)) as stream:
header_offset, headers = headers_guess(stream.sample)
has_logged_dialect = True
except TabulatorException:
try:
file_format = mimetype.lower().split('/')[-1]
with UnknownEncodingStream(table_filepath, file_format, decoding_result,
post_parse=[TypeConverter().convert_types], dialect=dialect,
dialect=dialect,
force_encoding=bool(encoding),
skip_rows=[{'type': 'preset', 'value': 'blank'}],
post_parse=[TypeConverter().convert_types],
logger=(logger if not has_logged_dialect else None)) as stream:
header_offset, headers = headers_guess(stream.sample)
has_logged_dialect = True
Expand All @@ -459,6 +464,7 @@ def load_table(table_filepath, resource_id, mimetype='text/csv', dialect=None, e
# 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()
# (canada fork only): add config option for strict guessing
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 d5078cd

Please sign in to comment.