python - Receiving "KeyError" after decoding json result from url -
I am new to Python I am trying to parse JSON result from a URL. Basically, I was using the following:
response = urllib.request.urlopen (url) json_obj = json.load (response) It should be a stroke "str 'not' bytes' in the lines of a given" JSON object, so after searching on the StackoverView Flo, I decode the response in this way:
F = urllib.request.urlopen (Url) charset = f.info (). Get_param ('charset', 'utf8') data = f.read () decoded = json.loads (data.decode (charset)) If I print "decode" I is as follows:
{ 'link': { 'summary data': 'https: // localhost / piwebapi / streams / p0_7qHaW4UHU-RlCaz8tpasAAQAAAAU0hJTExNQU42NDIwXFNJTlVTT0lE / summary' 'value': 'https: // localhost / Piwebapi / streams / P0_7qHaW4UHU-RlCaz8tpasAAQAAAAU0hJTExNQU42NDIwXFNJTlVTT0lE / price ',' InterpolatedData ':' https: // localhost / Piwebapi / streams / P0_7qHaW4UHU-RlCaz8tpasAAQAAAAU0hJTExNQU42NDIwXFNJTlVTT0lE / interpolated ',' self ':' https: // localhost / Pubabpe / Points / P0_7 q HAU4 UHU-RA Seas 8 Teepiasaaaaaaafaanattiajitielvititi 0 LEDs' Plow tData ':' https: // localhost / piwebapi / streams / P0_7qHaW4UHU-RlCaz8tpasAAQAAAAU0hJTExNQU42NDIwXFNJTlVTT0lE / plot ',' DataServer ':' https: // localhost / piwebapi / dataservers / s0_7qHaW4UHU- ',' 'Sinusoid', 'WebId': 'P0_7qHaW4UHU- RlCaz8tpasAAQAAAAU0hJTExNQU42NDIwXFNJTlVTT0lE '} I do now, even when I tried to remove a particular value, are I get a "Kunjiiyr" For example:
print (decode [0]) returns :
traceback (most recent call last): file "C: \ program files (x86) in the \ Microsoft Visual Studio 10.0 \ Common7 \ IDE Extensio ns \ Microsoft \ Visual Studio Python Tools \ 2.1 \ visualstudio_py_util.py" for line 1 06, exec_file exec_code (code, file, Vasvik_viriabls) file "C: \ program files (x86) \ Microsoft Visual Studio 10.0 \ Common7 \ IDE \ Extensio ns \ Microsoft \ Visual Studio \ Python tools Tools \ 2.1 \ visualstudio_py_util.py" for line 8 2, in exec_code exec (Code_obj, global_variables) f Ail "C: \ Users \ shillman \ Documents \ Visual Studio 2010 \ Projects \ PythonApplicatio n1 \ PythonApplication1 \ PythonApplication1.py", line 26, & lt; Module & gt; Print (decode [0]) keyier: 0 In which format is "decode"? Is there any way I can do this where it's actually a dictionary? Is there a way to convert it into a dictionary? Any help / advice is appreciated! Thank you.
in which format is "decode"? Is there any way I can do this where it's actually a dictionary? Is there a way to convert it into a dictionary?
According to what you have printed, is decoded a dictionary, and that is actually a problem. The keys of this dictionary are 'link' , 'name' , 'id' , and ' WebID '. But looking for a value with decoded [0] key 0 . There is no such key, so you get an error. It looks like what you really wanted, was a way of getting the first value, behaving like a dictionary, as if it was a list or other sequence you can Treat the items of the key, or values, or words of a word as a sequence, but not directly because: - The order of these things is completely arbitrary
- There is no such reason as a word
0 or 1 instead of 'links' or 'name' . - Therefore, the dragon will not let you do this by using the normal
[] operator. If you want to get the first value in the dictionary, then remember that this command is arbitrary, and "before" does not mean that what you want is you want to do it like this Can:
list (decode values)) [0] one ?? | Or less useless, instead of trying to make it, keep it in iterative form in a sequence:
next (iter (decoded.values ()) If you are still using Python 2.x, value () actually returns a list, so both of these are useless and unnecessary, you next ( Decoded.itervalue) want to use S ()) instead.
Comments
Post a Comment