Saver¶
- class torchdata.datapipes.iter.Saver(source_datapipe: IterDataPipe[Tuple[Any, Union[bytes, bytearray, str]]], mode: str = 'w', filepath_fn: Optional[Callable] = None)¶
接收元数据和数据元组的 DataPipe,将数据保存到由
filepath_fn
和元数据生成的 target path,并在本地文件系统上生成文件路径(函数名称:save_to_disk
)。- 参数:
source_datapipe – 元数据和数据元组的可迭代 DataPipe
mode – 用于写入数据的打开文件模式(默认为
"w"
)filepath_fn – 接收元数据并返回新文件目标路径的函数
示例
>>> from torchdata.datapipes.iter import IterableWrapper >>> import os >>> def filepath_fn(name: str) -> str: >>> return os.path.join(".", os.path.basename(name)) >>> name_to_data = {"1.txt": b"DATA1", "2.txt": b"DATA2", "3.txt": b"DATA3"} >>> source_dp = IterableWrapper(sorted(name_to_data.items())) >>> saver_dp = source_dp.save_to_disk(filepath_fn=filepath_fn, mode="wb") >>> res_file_paths = list(saver_dp) >>> res_file_paths ['./1.txt', './2.txt', './3.txt']