简体   繁体   中英

Trouble accessing python dictionary elements

I have the following dict:

b = {None: u'{"TimeStamp":"2017-01-30T13:50:22.3854765+00:00","RealEstate":          {"Id":"fa9ba4d0-3e69-46f0-9460-05d6b2ff1211","Name":"Building 1","Room":"Room     1","Device":{"Id":"fa9ba4d0-3e69-46f0-9460-08d6b2ff6408","Payload":{"Temperature":{"Unit":"C","Value":32.3},"Humidity":{"Unit":"%","Value":12},"Light":{"Unit":"lux","Value":154},"Motion":{"Unit":"Number of motion","Value":0},"Co2":null}}},"Message":null}'}

And cant access any elements. Tried both b['None'] , b['u'] and b['TimeStamp'] which all generates:

ERROR: ..../sparkstream.py", line 24, in test
    c = b['u']

KeyError: 'u'

Any tips!?

What you have aa dictionary with one key, which you can access the value via b[None] . This value in turn, is a string , not a dictionary. In order to get the dictionary, you will have to use JSON to decode it:

import json

b = {None: u'{"TimeStamp":"2017-01-30T13:50:22.3854765+00:00","RealEstate":          {"Id":"fa9ba4d0-3e69-46f0-9460-05d6b2ff1211","Name":"Building 1","Room":"Room     1","Device":{"Id":"fa9ba4d0-3e69-46f0-9460-08d6b2ff6408","Payload":{"Temperature":{"Unit":"C","Value":32.3},"Humidity":{"Unit":"%","Value":12},"Light":{"Unit":"lux","Value":154},"Motion":{"Unit":"Number of motion","Value":0},"Co2":null}}},"Message":null}'}
dict_object = json.loads(b[None])
print json.dumps(dict_object, indent=2)  # Show the decoded result

# Accessing some keys
print '---'
print 'Time stamp:', dict_object['TimeStamp']
print 'Real Estate ID:', dict_object['RealEstate']['Id']
print 'Device ID:', dict_object['RealEstate']['Device']['Id']

Output:

{
  "TimeStamp": "2017-01-30T13:50:22.3854765+00:00", 
  "Message": null, 
  "RealEstate": {
    "Device": {
      "Id": "fa9ba4d0-3e69-46f0-9460-08d6b2ff6408", 
      "Payload": {
        "Motion": {
          "Value": 0, 
          "Unit": "Number of motion"
        }, 
        "Light": {
          "Value": 154, 
          "Unit": "lux"
        }, 
        "Co2": null, 
        "Temperature": {
          "Value": 32.3, 
          "Unit": "C"
        }, 
        "Humidity": {
          "Value": 12, 
          "Unit": "%"
        }
      }
    }, 
    "Room": "Room     1", 
    "Id": "fa9ba4d0-3e69-46f0-9460-05d6b2ff1211", 
    "Name": "Building 1"
  }
}
---
Time stamp: 2017-01-30T13:50:22.3854765+00:00
Real Estate ID: fa9ba4d0-3e69-46f0-9460-05d6b2ff1211
Device ID: fa9ba4d0-3e69-46f0-9460-08d6b2ff6408

The Dictionary which you shared has only one key, an object of type None. To access it you simply type: b[None] the value returned from that call will be the whole string prefixed with u'...' .

You can't access the elements in it because this is just a Unicode string, not a python dictionary. You need to convert this JSON string to a dictionary and you can find instructions here:

https://pythonspot.com/json-encoding-and-decoding-with-python/

Hope this helps

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM