如何从python中返回的json文件中获取特定密钥?
我在python中从json文件获取某个密钥时遇到问题。我真的不知道从哪里开始,因为我以前从未在python中使用json如何从python中返回的json文件中获取特定密钥?,python,json,Python,Json,我在python中从json文件获取某个密钥时遇到问题。我真的不知道从哪里开始,因为我以前从未在python中使用json [ { "author": "Chinua Achebe", "country": "Nigeria", "imageLink": "images/things-fall-apart.jpg", "language": "English", "link": "https://en.wikipedia.org/wiki/Things
[
{
"author": "Chinua Achebe",
"country": "Nigeria",
"imageLink": "images/things-fall-apart.jpg",
"language": "English",
"link": "https://en.wikipedia.org/wiki/Things_Fall_Apart\n",
"pages": 209,
"title": "Things Fall Apart",
"year": 1958
},
{
"author": "Hans Christian Andersen",
"country": "Denmark",
"imageLink": "images/fairy-tales.jpg",
"language": "Danish",
"link": "https://en.wikipedia.org/wiki/Fairy_Tales_Told_for_Children._First_Collection.\n",
"pages": 784,
"title": "Fairy tales",
"year": 1836
}
]
这是json文件中的几行代码作为示例
import json
f = open('**\\booksset1.json')
data = json.load(f)
这一部分加载json文件,但是如何从整个文件中获取一个标题。使用for循环访问键和值。 比如说
import json
f = open('**\\booksset1.json')
data = json.load(f)
Print First item title:
print data[0]['title']
To iterate all the of the author details
for d in data:
print d['author]
print d['title']
json.load
已将文本字符串表示形式转换为(在本例中)DICT列表。因此,要访问第二个(童话故事)的年份,请使用
您可以执行所有常见的python操作,例如迭代列表中的所有dict:
for d in data:
author, title, date = d['author'], d['title'], d['year']
print( f'{author} wrote "{title}" in {year}')
如果您仍然有问题,请尝试使用
以下是您可以使用的一些其他技巧:
f[0]["author"] = "Foo Man" # Set the value
f.json() # Get object from the JSON file
您可以找到更多信息。这将使您基本了解如何在python中使用JSON
import json
f = open('booksset1.json')
data = json.load(f)
# for single
print("Aurthor name :",data[0]['author'])
print("Country name :",data[0]['country'])
print()
# for multiple you should use loop
for i in data:
print(i['title'])
您可以使用该函数从字典列表中获取键的值,如下所示:
data = [
{
"author": "Chinua Achebe",
"country": "Nigeria",
"imageLink": "images/things-fall-apart.jpg",
"language": "English",
"link": "https://en.wikipedia.org/wiki/Things_Fall_Apart\n",
"pages": 209,
"title": "Things Fall Apart",
"year": 1958
},
{
"author": "Hans Christian Andersen",
"country": "Denmark",
"imageLink": "images/fairy-tales.jpg",
"language": "Danish",
"link": "https://en.wikipedia.org/wiki/Fairy_Tales_Told_for_Children._First_Collection.\n",
"pages": 784,
"title": "Fairy tales",
"year": 1836
}
]
titles = [item.get("title", "") for item in data]
输出:
['Things Fall Apart', 'Fairy tales']
如果您喜欢在数据框中使用此数据,并且由于数据的行为类似于字典列表,您可以尝试:
import pandas as pd
df = pd.DataFrame.from_dict(data)
df
author country ... title year
0 Chinua Achebe Nigeria ... Things Fall Apart 1958
1 Hans Christian Andersen Denmark ... Fairy tales 1836
您的数据
是一个dict列表,类似于JSON中的dict。您可以获得第一个标题,就像正常的目录列表一样:data[0]['title']
data = [
{
"author": "Chinua Achebe",
"country": "Nigeria",
"imageLink": "images/things-fall-apart.jpg",
"language": "English",
"link": "https://en.wikipedia.org/wiki/Things_Fall_Apart\n",
"pages": 209,
"title": "Things Fall Apart",
"year": 1958
},
{
"author": "Hans Christian Andersen",
"country": "Denmark",
"imageLink": "images/fairy-tales.jpg",
"language": "Danish",
"link": "https://en.wikipedia.org/wiki/Fairy_Tales_Told_for_Children._First_Collection.\n",
"pages": 784,
"title": "Fairy tales",
"year": 1836
}
]
titles = [item.get("title", "") for item in data]
['Things Fall Apart', 'Fairy tales']
import pandas as pd
df = pd.DataFrame.from_dict(data)
df
author country ... title year
0 Chinua Achebe Nigeria ... Things Fall Apart 1958
1 Hans Christian Andersen Denmark ... Fairy tales 1836