Python Forum
How to take a single value from dictionary
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to take a single value from dictionary
#1
I've created codes that collects weather data using JSON and API keys. However, all I really need is one value of one dictionary, but my code keeps returning "TypeError: list indices must be integers or slices, not str". What do I do.

Relevant Code:
url = "http://api.openweathermap.org/data/2.5/weather?q=brisbane&appid=7f50acdcb4d66b2117f7af48d489fbd4"
response = urllib.request.urlopen(url)
result_3 = json.loads(response.read())
weather = result_3['weather']['description']
print("ISS now at latitude: ", weather)
'weather' dictionary:[{'id': 800, 'main': 'Clear', 'description': 'clear sky', 'icon': '01n'}]
Reply
#2
weather = result_3['weather'][0]['description']
weather is a list with one element (at least in this case). Maybe in other cases it could have more than one lement
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  convert List with dictionaries to a single dictionary iamaghost 3 2,850 Jan-22-2021, 03:56 PM
Last Post: iamaghost
  PyMySQL return a single dictionary Valon1981 2 1,770 Feb-20-2020, 04:07 PM
Last Post: Valon1981

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020