JsonParser¶
- class torchdata.datapipes.iter.JsonParser(source_datapipe: IterDataPipe[Tuple[str, IO]], **kwargs)¶
从 JSON 数据流中读取并生成文件名和 JSON 数据的元组(函数名称:
parse_json_files
)。- 参数:
source_datapipe – 包含文件名和 JSON 数据流元组的 DataPipe
kwargs – 将传递给
json.loads
的关键字参数
示例
>>> from torchdata.datapipes.iter import IterableWrapper, FileOpener >>> import os >>> def get_name(path_and_stream): >>> return os.path.basename(path_and_stream[0]), path_and_stream[1] >>> datapipe1 = IterableWrapper(["empty.json", "1.json", "2.json"]) >>> datapipe2 = FileOpener(datapipe1, mode="b") >>> datapipe3 = datapipe2.map(get_name) >>> json_dp = datapipe3.parse_json_files() >>> list(json_dp) [('1.json', ['foo', {'bar': ['baz', None, 1.0, 2]}]), ('2.json', {'__complex__': True, 'real': 1, 'imag': 2})]