嵌套数据到列表
Nested Data into List
f = open("sample_diction.json","r")
sample_photo_rep = json.loads(f.read())
print sample_photo_rep
f.close()
以上是我的代码正在打开文件 sample_diction.json 并将其内容作为 Python 对象加载到变量 sample_photo_rep 中。我尝试做的下一步是编写代码来访问 sample_flickr_obj 中的嵌套数据,以创建该照片的所有标签列表(“sample_diction.json”中的数据)。然后我想将标签列表保存在一个名为 sample_tags_list 的变量中。
以下是 sample_diction.json 中包含的数据......我只是不确定从哪里开始。任何帮助,将不胜感激。
{
"photo": {
"people": {
"haspeople": 0
},
"dateuploaded": "1467709435",
"owner": {
"username": "Ansel Adams",
"realname": "",
"nsid": "48093195@N03",
"iconserver": "7332",
"location": "",
"path_alias": null,
"iconfarm": 8
},
"publiceditability": {
"canaddmeta": 1,
"cancomment": 1
},
"id": "27820301400",
"title": {
"_content": "Photo1"
},
"media": "photo",
"tags": {
"tag": [
{
"machine_tag": false,
"_content": "nature",
"author": "48093195@N03",
"raw": "Nature",
"authorname": "ac | photo albums",
"id": "48070141-27820301400-5470"
},
{
"machine_tag": false,
"_content": "mist",
"author": "48093195@N03",
"raw": "Mist",
"authorname": "ac | photo albums",
"id": "48070141-27820301400-852"
},
{
"machine_tag": false,
"_content": "mountain",
"author": "48093195@N03",
"raw": "Mountain",
"authorname": "ac | photo albums",
"id": "48070141-27820301400-1695"
}
]
},
"comments": {
"_content": "0"
},
"secret": "c86034becf",
"usage": {
"canblog": 0,
"canshare": 1,
"candownload": 0,
"canprint": 0
},
"description": {
"_content": ""
},
"isfavorite": 0,
"views": "4",
"farm": 8,
"visibility": {
"isfriend": 0,
"isfamily": 0,
"ispublic": 1
},
"rotation": 0,
"dates": {
"taken": "2016-07-05 11:03:52",
"takenunknown": "1",
"takengranularity": 0,
"lastupdate": "1467709679",
"posted": "1467709435"
},
"license": "0",
"notes": {
"note": []
},
"server": "7499",
"safety_level": "0",
"urls": {
"url": [
{
"type": "photopage",
"_content": "https://www.flickr.com/photos/48093195@N03/27820301400/"
}
]
},
"editability": {
"canaddmeta": 0,
"cancomment": 0
}
},
"stat": "ok"
}
嵌套数据一开始可能会让人望而生畏,但如果你一步一步来,就会很容易。
您从 sample_photo_re
开始。是字典,只有一个键。
所以 :
sample_photo_rep["photo"]
这是另一个字典。有趣的信息似乎在 tags
:
sample_photo_rep["photo"]["tags"]
又一个字典。所以:
sample_photo_rep["photo"]["tags"]["tag"]
这次是一个列表,所以你可以对其进行迭代:
for tag in sample_photo_rep["photo"]["tags"]["tag"]:
print tag
它输出:
{'machine_tag': False, '_content': 'nature', 'author': '48093195@N03', 'raw': 'Nature', 'authorname': 'ac | photo albums', 'id': '48070141-27820301400-5470'}
{'machine_tag': False, '_content': 'mist', 'author': '48093195@N03', 'raw': 'Mist', 'authorname': 'ac | photo albums', 'id': '48070141-27820301400-852'}
{'machine_tag': False, '_content': 'mountain', 'author': '48093195@N03', 'raw': 'Mountain', 'authorname': 'ac | photo albums', 'id': '48070141-27820301400-1695'}
那些都是字典。您可能只对 raw
键感兴趣,所以:
for tag in sample_photo_rep["photo"]["tags"]["tag"]:
print tag['raw']
# Nature
# Mist
# Mountain
完成!
如果您遇到错误(例如 KeyError: 'row'
),请退一步查看数据,可能使用 type(object)
查看它是列表还是字典。
更新:要获得 [u'nature', u'mist', u'mountain']
:
[unicode(tag) for tag in sample_photo_rep["photo"]["tags"]["tag"]]
f = open("sample_diction.json","r")
sample_photo_rep = json.loads(f.read())
print sample_photo_rep
f.close()
以上是我的代码正在打开文件 sample_diction.json 并将其内容作为 Python 对象加载到变量 sample_photo_rep 中。我尝试做的下一步是编写代码来访问 sample_flickr_obj 中的嵌套数据,以创建该照片的所有标签列表(“sample_diction.json”中的数据)。然后我想将标签列表保存在一个名为 sample_tags_list 的变量中。 以下是 sample_diction.json 中包含的数据......我只是不确定从哪里开始。任何帮助,将不胜感激。
{
"photo": {
"people": {
"haspeople": 0
},
"dateuploaded": "1467709435",
"owner": {
"username": "Ansel Adams",
"realname": "",
"nsid": "48093195@N03",
"iconserver": "7332",
"location": "",
"path_alias": null,
"iconfarm": 8
},
"publiceditability": {
"canaddmeta": 1,
"cancomment": 1
},
"id": "27820301400",
"title": {
"_content": "Photo1"
},
"media": "photo",
"tags": {
"tag": [
{
"machine_tag": false,
"_content": "nature",
"author": "48093195@N03",
"raw": "Nature",
"authorname": "ac | photo albums",
"id": "48070141-27820301400-5470"
},
{
"machine_tag": false,
"_content": "mist",
"author": "48093195@N03",
"raw": "Mist",
"authorname": "ac | photo albums",
"id": "48070141-27820301400-852"
},
{
"machine_tag": false,
"_content": "mountain",
"author": "48093195@N03",
"raw": "Mountain",
"authorname": "ac | photo albums",
"id": "48070141-27820301400-1695"
}
]
},
"comments": {
"_content": "0"
},
"secret": "c86034becf",
"usage": {
"canblog": 0,
"canshare": 1,
"candownload": 0,
"canprint": 0
},
"description": {
"_content": ""
},
"isfavorite": 0,
"views": "4",
"farm": 8,
"visibility": {
"isfriend": 0,
"isfamily": 0,
"ispublic": 1
},
"rotation": 0,
"dates": {
"taken": "2016-07-05 11:03:52",
"takenunknown": "1",
"takengranularity": 0,
"lastupdate": "1467709679",
"posted": "1467709435"
},
"license": "0",
"notes": {
"note": []
},
"server": "7499",
"safety_level": "0",
"urls": {
"url": [
{
"type": "photopage",
"_content": "https://www.flickr.com/photos/48093195@N03/27820301400/"
}
]
},
"editability": {
"canaddmeta": 0,
"cancomment": 0
}
},
"stat": "ok"
}
嵌套数据一开始可能会让人望而生畏,但如果你一步一步来,就会很容易。
您从 sample_photo_re
开始。是字典,只有一个键。
所以 :
sample_photo_rep["photo"]
这是另一个字典。有趣的信息似乎在 tags
:
sample_photo_rep["photo"]["tags"]
又一个字典。所以:
sample_photo_rep["photo"]["tags"]["tag"]
这次是一个列表,所以你可以对其进行迭代:
for tag in sample_photo_rep["photo"]["tags"]["tag"]:
print tag
它输出:
{'machine_tag': False, '_content': 'nature', 'author': '48093195@N03', 'raw': 'Nature', 'authorname': 'ac | photo albums', 'id': '48070141-27820301400-5470'}
{'machine_tag': False, '_content': 'mist', 'author': '48093195@N03', 'raw': 'Mist', 'authorname': 'ac | photo albums', 'id': '48070141-27820301400-852'}
{'machine_tag': False, '_content': 'mountain', 'author': '48093195@N03', 'raw': 'Mountain', 'authorname': 'ac | photo albums', 'id': '48070141-27820301400-1695'}
那些都是字典。您可能只对 raw
键感兴趣,所以:
for tag in sample_photo_rep["photo"]["tags"]["tag"]:
print tag['raw']
# Nature
# Mist
# Mountain
完成!
如果您遇到错误(例如 KeyError: 'row'
),请退一步查看数据,可能使用 type(object)
查看它是列表还是字典。
更新:要获得 [u'nature', u'mist', u'mountain']
:
[unicode(tag) for tag in sample_photo_rep["photo"]["tags"]["tag"]]