Skip to content
Merged
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
11 changes: 9 additions & 2 deletions src/crawlee/storages/dataset.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import csv
import io
import json
import logging
from typing import TYPE_CHECKING, AsyncIterator, Literal, TextIO, TypedDict, cast

from typing_extensions import NotRequired, Required, Unpack, override
Expand All @@ -20,6 +21,9 @@
from crawlee.types import JSONSerializable


logger = logging.getLogger(__name__)


class GetDataKwargs(TypedDict):
"""Keyword arguments for dataset's `get_data` method.

Expand Down Expand Up @@ -209,8 +213,11 @@ async def write_to(self, content_type: Literal['json', 'csv'], destination: Text
offset += list_items.count

if content_type == 'csv':
writer = csv.writer(destination, quoting=csv.QUOTE_MINIMAL)
writer.writerows([items[0].keys(), *[item.values() for item in items]])
if items:
writer = csv.writer(destination, quoting=csv.QUOTE_MINIMAL)
writer.writerows([items[0].keys(), *[item.values() for item in items]])
else:
logger.warning('Attempting to export an empty dataset - no file will be created')
elif content_type == 'json':
json.dump(items, destination)
else:
Expand Down