当前位置: 首页>>编程示例 >>用法及示例精选 >>正文


Python PyTorch JsonParser用法及代码示例

本文简要介绍python语言中 torchdata.datapipes.iter.JsonParser 的用法。

用法:

class torchdata.datapipes.iter.JsonParser(source_datapipe: IterDataPipe[Tuple[str, IO]], **kwargs)

参数

  • source_datapipe-带有文件名和 JSON 数据流元组的 DataPipe

  • kwargs-将传递给json.loads的关键字参数

从 JSON 数据流读取并生成文件名和 JSON 数据的元组(函数名称:parse_json_files )。

示例

>>> 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})]

相关用法


注:本文由纯净天空筛选整理自pytorch.org大神的英文原创作品 torchdata.datapipes.iter.JsonParser。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。