DevelopmentLanguage:Python:Dict01
20.辞書型(Python)
前提
以下のjson形式のデータを辞書として利用して記載します。
json_data = { "s3": [ { "s3SchemaVersion": "1.0", "configurationId": "aac92357-06c7-49ab-8aa3-86e163aa4e12", "bucket": { "name": "s3-bucket", "ownerIdentity": { "principalId": "A10X0TF2OAHF1S" } } } ] }
Key, Valueの取り出し
for k, v in json_data.items(): pass print(k, '\n', v) # s3 # [{'s3SchemaVersion': '1.0', 'configurationId': 'aac92357-06c7-49ab-8aa3-86e163aa4e12', 'bucket': {'name': 's3-bucket', 'ownerIdentity': {'principalId': 'A10X0TF2OAHF1S'}}}]
Value(list)から取り出し
v[0].keys() # dict_keys(['s3SchemaVersion', 'configurationId', 'bucket']) v[0].values() # dict_values(['1.0', 'aac92357-06c7-49ab-8aa3-86e163aa4e12', {'name': 's3-bucket', 'ownerIdentity': {'principalId': 'A10X0TF2OAHF1S'}}]) key_list = list(v[0].keys()) key_list[0] # 's3SchemaVersion' for _ in list(v[0].keys()): print(_) # s3SchemaVersion # configurationId # bucket value_list = list(v[0].values()) value_list[0] # '1.0' for _ in list(v[0].values()): print(_) # 1.0 # aac92357-06c7-49ab-8aa3-86e163aa4e12 # {'name': 's3-bucket', 'ownerIdentity': {'principalId': 'A10X0TF2OAHF1S'}}
DevelopmentLanguage/Python/Dict01.txt · 最終更新: 2023/01/05 by admin