python select key in dict



有一个简单的请求到Solr下面

response = requests.get("http://localhost:8080/solr/select?fl=id,title,description,keywords,author&q=domain:mydomain.com&rows=10&indent=on&wt=json&omitHeader=true")
result = response.json()
print type(result)
print result
print response.text

上面的代码给了我以下输出

类型
<type 'dict'>

字典内容

{u'response': {u'start': 0, u'numFound': 1, u'docs': [{u'keywords': u'my keywords listed here', u'id': u'project.websiteindex.abe919664893e46eef21aacb1360948ae836a756faa28e2063a4a92ef4273630', u'description': u'my site description isted here.', u'title': u'my site title'}]}}

及以下可读性更好的版本

{
"response":{"numFound":1,"start":0,"docs":[
{
"description":"my site description isted here.",
"title":"my site title",
"keywords":"my keywords listed here",
"id":"project.websiteindex.abe919664893e46eef21aacb1360948ae836a756faa28e2063a4a92ef4273630"}]
}}

执行:

print(result['response']['numFound'])

将给我非常有用的输出

1

非常好,但是显然我想要一些像

这样的东西
print(result['response']['title'])
my site title

所以我的问题,我如何得到我的标题,描述和关键字的值作为关键字如何使用这种格式选择它们,或者如何以更好的字典格式获得所需的值?

注。这里使用python 2.7和solr 3.6的旧设置

由于您要寻址的值是数组元素的字段,因此您必须首先寻址该元素。

试试这样写:

result['response']['docs'][0]['title']

最新更新