當前位置: 首頁>>編程示例 >>用法及示例精選 >>正文


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。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。