Skip to content
Open
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
2 changes: 2 additions & 0 deletions sqlite_utils/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -379,6 +379,8 @@ class Format(enum.Enum):
raise TypeError(
"rows_from_file() requires a file-like object that supports peek(), such as io.BytesIO"
)
if not first_bytes:
return iter([]), Format.CSV
if first_bytes.startswith(b"[") or first_bytes.startswith(b"{"):
# TODO: Detect newline-JSON
return rows_from_file(buffered, format=Format.JSON)
Expand Down
7 changes: 7 additions & 0 deletions tests/test_rows_from_file.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,3 +52,10 @@ def test_rows_from_file_error_on_string_io():
assert ex.value.args == (
"rows_from_file() requires a file-like object that supports peek(), such as io.BytesIO",
)


@pytest.mark.parametrize("content", [b"", b" \n\n "])
def test_rows_from_file_empty_file(content):
rows, fmt = rows_from_file(BytesIO(content))
assert list(rows) == []
assert fmt == Format.CSV
Loading