项目中:Json文件的读取

本文通过一个具体的示例详细介绍了如何在Python中使用json库来读取和解析JSON文件,并展示了如何提取文件中的关键内容,包括sentids、sentences、split和filename等信息。此外,还提供了查看JSON内容类型、所有关键词及其对应的值的方法,帮助理解JSON数据结构的处理。
摘要由CSDN通过智能技术生成

项目中:Json文件的读取

  • 举例:Json文件内容如下(Flickr8k)
{'images':
     [{'sentids': [39300, 39301, 39302, 39303, 39304],
        'imgid': 7860,
        'sentences': [{'tokens': ['a', 'girl', 'in', 'blue', 'is', 'jumping', 'on', 'the', 'shore', 'as', 'small', 'waves', 'approach', 'her'],
                        'raw': 'A girl in blue is jumping on the shore as small waves approach her .', 'imgid': 7860, 'sentid': 39300},
                      {'tokens': ['a', 'girl', 'leaps', 'into', 'the', 'air', 'while', 'standing', 'by', 'the', 'ocean'],
                        'raw': 'A girl leaps into the air while standing by the ocean .', 'imgid': 7860, 'sentid': 39301},
                      {'tokens': ['a', 'young', 'long', 'haired', 'girl', 'on', 'the', 'beach', 'is', 'jumping', 'in', 'the', 'air'],
                        'raw': 'A young , long-haired , girl on the beach , is jumping in the air .', 'imgid': 7860, 'sentid': 39302},
                      {'tokens': ['the', 'girl', 'is', 'running', 'into', 'the', 'ocean', 'from', 'the', 'shore'],
                        'raw': 'The girl is running into the ocean from the shore .', 'imgid': 7860, 'sentid': 39303},
                      {'tokens': ['the', 'girls', 'is', 'jumping', 'into', 'the', 'air', 'on', 'the', 'beach'],
                        'raw': 'The girls is jumping into the air on the beach .', 'imgid': 7860, 'sentid': 39304}
                      ],
        'split': 'test',
        'filename': '1174525839_7c1e6cfa86.jpg'}
      ],
'dataset': 'flickr8k'}

读Json文件

import json
f = json.load("文件位置")# 读Json文件

取Json文件中内容

和列表/字典取元素一样,如下示例:

举例:

import json

result = json.loads('{"images":[{"sentids":[39300, 39301],"sentences":[{"raw":"A girl in blue"}],"spilt":"test","filename":"1.jpg"}],"dataset": "flickr8k"}')
# 查看json文件内容类型:
print("json文件内容类型: \n",type(result)) # <class 'dict'>
# 查看json文件内容:
print("json文件内容:   \n",result)

# 查看文件所有的关键词key:
key = result.keys()
print("文件所有的关键词key:\n",key) # dict_keys(['images', 'dataset'])
# 查看文件所有关键词key对应的值value,只显示value,不显示key
value = result.values()
print("文件所有关键词key对应的值value,只显示value,不显示key\n",value)
# 文件为字典,查看key中value的key的value:Eg:查看images中sentids对应的value
sentids_value = result["images"][0]["sentids"]
print("查看images中sentids对应的value\n",sentids_value)
# 文件为字典,查看key中value的key的value:Eg:查看images中sentences中raw对应的value
sentences_value = result["images"][0]["sentences"][0]["raw"]
print("查看images中sentences对应的value\n",sentences_value)

输出结果:

json文件内容类型: 
 <class 'dict'>
json文件内容:   
 {'images': [{'sentids': [39300, 39301], 'sentences': [{'raw': 'A girl in blue'}], 'spilt': 'test', 'filename': '1.jpg'}], 'dataset': 'flickr8k'}
文件所有的关键词key:
 dict_keys(['images', 'dataset'])
文件所有关键词key对应的值value,只显示value,不显示key
 dict_values([[{'sentids': [39300, 39301], 'sentences': [{'raw': 'A girl in blue'}], 'spilt': 'test', 'filename': '1.jpg'}], 'flickr8k'])
查看images中sentids对应的value
 [39300, 39301]
查看images中sentences对应的value
 A girl in blue
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Laura_Wangzx

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值