代码中的Python错误-r.json()[“输出"] [“名称"] TypeError:列表索引必须是整数或切片,而不是str [英] Python error in code - r.json()["output"]["name"] TypeError: list indices must be integers or slices, not str
本文介绍了代码中的Python错误-r.json()[“输出"] [“名称"] TypeError:列表索引必须是整数或切片,而不是str的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在获取JSON API响应,该响应将转换为Python字典,以便可以在Pandas的数据框中进行访问.我从API收到的响应是:
I am taking JSON API response, which gets converted into Python dictionary so that it can be accessed in a dataframe in Pandas. The response I receive from the API is:
{"output":
[
{"searchable_values":"top auxiliary nursing midwifery anm colleges in india",
"score":42.311504,
"url":"courses\/auxiliary-nursing-midwifery-anm-nursing-colleges-in-india",
"entity_id":"285999",
"count":136,
"count_scale":1.184174624829468,
"search_short_form2":null,
"item_type":"College Listing",
"name":"Top Auxiliary Nursing Midwifery [ANM] (Nursing) Colleges In India"},
{"searchable_values":"top auxiliary nursing midwifery anm colleges in gujarat",
"score":42.006725,
"url":"courses\/auxiliary-nursing-midwifery-anm-nursing-colleges-in-gujarat",
"entity_id":"298571",
"count":15,
"count_scale":1.019099590723056,
"search_short_form2":null,
"item_type":"College Listing",
"name":"Top Auxiliary Nursing Midwifery [ANM] (Nursing) Colleges In Gujarat"
}
]
}
到目前为止的代码是:
import requests
import pandas as pd
from pandas.io.json import json_normalize
import json
url = "https://collegedunia.com/global-search?term=a"
r = requests.get(url)
r.json()
r.json()["output"]["name"]
name = r.json()["output"]["name"]
df = pd.DataFrame.from_dict(name)
df.head()
现在,我收到此错误-
Traceback (most recent call last): File
"/Applications/site-search-analytics-2.py", line 15, in <module>
r.json()["output"]["name"] TypeError: list indices must be integers or slices, not str
如何解决此错误?
推荐答案
对我来说,省略name
:
url = "https://collegedunia.com/global-search?term=a"
r = requests.get(url)
df = pd.DataFrame(r.json()['output'])
print (df.head())
searchable_values score \
0 iadc a indian academy degree college karnataka... 20.989530
1 mohamed sathak a j college of pharmacy tamil n... 20.985674
2 chitransh a d p g college madhya bhopal adpg 19.891867
3 s a t i samrat ashok technological institute m... 19.079887
4 iimb indian institute of management karnataka ... 17.247448
url entity_id count \
0 college/1959-indian-academy-degree-college-iad... 1959 6.27180
1 college/9700-mohamed-sathak-aj-college-of-phar... 9700 7.49870
2 college/839-chitransh-adpg-college-bhopal 839 5.13000
3 college/14910-samrat-ashok-technological-insti... 14910 4.72381
4 university/25602-indian-institute-of-managemen... 25602 10.00000
count_scale search_short_form2 item_type \
0 6.644620 iadc-a college
1 7.748830 None college
2 5.617000 None college
3 5.251429 s.a.t.i college
4 10.000000 iimb college
logo tab \
0 https://images.collegedunia.com/public/college... home
1 https://images.collegedunia.com/public/college... home
2 https://images.collegedunia.com/public/college... home
3 https://images.collegedunia.com/public/college... home
4 https://images.collegedunia.com/public/college... home
name \
0 Indian Academy Degree College - [IADC-A], Bang...
1 Mohamed Sathak A.J. College of Pharmacy, Chennai
2 Chitransh A.D.P.G. College, Bhopal
3 Samrat Ashok Technological Institute - [S.A.T...
4 Indian Institute of Management - [IIMB], Banga...
tabs
0 [home, courses-fees, gallery, placement, faculty]
1 [home, courses-fees, gallery, faculty, admission]
2 [home, courses-fees, gallery, placement, faculty]
3 [home, courses-fees, gallery, placement, faculty]
4 [home, courses-fees, gallery, placement, faculty]
这篇关于代码中的Python错误-r.json()[“输出"] [“名称"] TypeError:列表索引必须是整数或切片,而不是str的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文