Python解析字典

时间:2015-11-05 12:51:02

标签: python gcloud

我有这个,

from gcloud import datastore

dataset = datastore.Client(dataset_id='wifi-XXX')
entity = datastore.Entity(key=dataset.key('TokenEntity'))
query = dataset.query(kind='TokenEntity')
query.add_filter('name', '>', '')
for i in query.fetch():
    print i

当我使用它时,结果是:

   <Entity[{'kind': u'TokenEntity', 'name': u'z7jm0uRV'}] {u'createdOn': datetime.datetime(2015, 11, 4, 9, 0, 51, 830000, tzinfo=<UTC>), u'used': False, u'createdBy': 
    u'jiri.m@veolia.com', u'ssid': u'XXX-Public', u'usedOn': None}>

<Entity[{'kind': u'TokenEntity', 'name': u'z8bL1sVV'}] {u'createdOn': datetime.datetime(2015, 11, 4, 9, 2, 20, 240000, tzinfo=<UTC>), u'used': False, u'createdBy': 
    u'jiri.s@xxx.com', u'ssid': u'XXX-Public', u'usedOn': None}>

<Entity[{'kind': u'TokenEntity', 'name': u'zKVzBvDV'}] {u'createdOn': datetime.datetime(2015, 10, 22, 17, 1, 54, 817000, tzinfo=<UTC>), u'used': False, u'createdBy' : u'jaromir.s@vvv.com', u'ssid': u'XXX-Public', u'usedOn': None}>


I can use:

>>> i['used']
False

>>> i['createdBy']
u'jaromir.s@xxx.com'
>>> i['ssid']
u'XXX-Public'
>>> 

但是不可能得到'名字'

  
    
      

我[ '名称']           Traceback(最近一次调用最后一次):             文件“”,第1行,in           KeyError:'name'

    
  

谢谢..

1 个答案:

答案 0 :(得分:0)

我没有使用gcloud平台,但这就是我所看到的:

密钥方法kind()和id()从密钥中恢复实体的种类和标识符:

所以,试试吧:

i.kind()
i.id()
i.name()