Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 17 additions & 8 deletions src/hdx/utilities/saver.py
Original file line number Diff line number Diff line change
Expand Up @@ -279,6 +279,7 @@ def save_iterable(
format: str = "csv",
encoding: str | None = None,
row_function: Callable[[dict], dict | None] | None = None,
no_empty: bool = True,
) -> list:
"""Save an iterable of rows in dict or list form to a csv. (The headers
argument is either a row number (rows start counting at 1), or the actual
Expand All @@ -293,6 +294,7 @@ def save_iterable(
format: Format to write. Defaults to csv.
encoding: Encoding to use. Defaults to None (infer encoding).
row_function: Row function to call for each row. Defaults to None.
no_empty: Don't save file if there are no data rows. Defaults to True.

Returns:
List of rows written to file
Expand All @@ -302,12 +304,26 @@ def save_iterable(
def row_function(row):
return row

def write_rows(newrows, has_header, headers) -> None:
resource = get_frictionless_tableresource(
data=newrows,
has_header=has_header,
headers=headers,
encoding=encoding,
)
resource.write(filepath, format=format, encoding=encoding)
resource.close()

newrows = []
rows = iter(rows)
try:
row = next(rows)
except StopIteration:
if not no_empty and headers:
newrows = [headers]
write_rows(newrows, None, None)
return newrows

if isinstance(row, dict):
has_header = True
row = row_function(row)
Expand Down Expand Up @@ -371,12 +387,5 @@ def row_function(row):
if row is None:
continue
newrows.append(row)
resource = get_frictionless_tableresource(
data=newrows,
has_header=has_header,
headers=headers,
encoding=encoding,
)
resource.write(filepath, format=format, encoding=encoding)
resource.close()
write_rows(newrows, has_header, headers)
return newrows
9 changes: 9 additions & 0 deletions tests/hdx/utilities/test_saver.py
Original file line number Diff line number Diff line change
Expand Up @@ -521,3 +521,12 @@ def row_func(row):

rows = save_iterable(filepath, [], headers=["h1", "h3", "h4"])
assert rows == []

rows = save_iterable(
filepath, [], headers=["h1", "h3", "h4"], no_empty=False
)
assert rows == [["h1", "h3", "h4"]]

newll = read_list_from_csv(str(filepath))
remove(filepath)
assert newll == [["h1", "h3", "h4"]]
Loading