Python Forum
want to print the value of a key
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
want to print the value of a key
#2
>>> import json 
>>> s = b'{"documents":[{"id":"1","detectedLanguages":[{"name":"English","iso6391Name":"en","score":1.0}]}],"errors":[]}'
>>> data = json.loads(s)
Traceback (most recent call last):
 File "<string>", line 301, in runcode
 File "<interactive input>", line 1, in <module>
 File "C:\Python34\lib\json\__init__.py", line 312, in loads
   s.__class__.__name__))
TypeError: the JSON object must be str, not 'bytes'

>>> # So convert to string
>>> s = s.decode()
>>> data = json.loads(s)
>>> data
{'documents': [{'detectedLanguages': [{'iso6391Name': 'en',
                                       'name': 'English',
                                       'score': 1.0}],
               'id': '1'}],
'errors': []}
# Get score
>>> data['documents'][0]['detectedLanguages'][0]['score']
1.0
I your other post i recommend you to use Requests.
Then you don't need to use json from standard library,or convert from bytes to string.
Reply


Messages In This Thread
RE: want to print the value of a key - by snippsat - Jan-22-2017, 05:57 PM

Forum Jump:

User Panel Messages

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