Home > Software engineering >  python select key in dict
python select key in dict

Time:05-24

have a simple request to Solr below

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

the above code fives me the following output

the type

<type 'dict'>

the dict contents

{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'}]}}

and below the better readable vesion

{
  "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"}]
  }}

executing:

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

will give me the very useful output of

1

Very nice and all, but obviously i would like to get something like

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

my site title

So my question, how do i get my values of title,description and keywords as keys How do i select them using this format, or how do i get my desired values in a better dict format?

p.s. stuck wioth an old setup here with python 2.7 and solr 3.6

CodePudding user response:

Since the values you are addressing are fields of an array element, you have to address the element first.

Try something like:

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