【问题标题】:Retrieve value from list from google datastore python从谷歌数据存储python的列表中检索值
【发布时间】:2018-09-24 06:48:08
【问题描述】:

我在 python 中有一个列表,它是对 Google 数据存储区的查询的产物,该列表如下所示:

[<Entity('User', 1111) {'refreshToken': 'xxx', 'firstName': 'Bill', 'lastName': 'Last', 'accessToken': 'xxx', 'idToken': 'xxx', 'email': 'the_email'}>]

我需要提取实体的 ID,即1111。到目前为止,我已经尝试了以下但没有成功:

result = list(query.fetch())
print(result[0][0]) #fails
print(result[0].Entity) #fails
print(result[0]['User']) #fails
print(result['User']) #fails

知道如何检索 ID 值吗?

【问题讨论】:

    标签: python google-cloud-datastore


    【解决方案1】:

    【讨论】:

    • 这行得通,我不确定是不是因为我现在极度缺乏睡眠,但你能忍受我并解释一下这实际上是如何从列表中获取 id 的吗?跨度>
    • 列表中的项目是“实体”类的实例。实体具有属性“id”
    • 我明白了,谢谢!我希望文档对这些内容更加明确。
    【解决方案2】:

    当您通过 fetch 查询返回 Entity 对象时。您可以像字典一样访问信息。

    # the line that said print(result["User"]) works
    # the problem is that the key you used may not be the exact wording as in the data store entity
    entity = list(query.fetch())[0]
    print(entity['user']) #works just be careful for the key you pass in the []
    

    【讨论】:

      猜你喜欢
      • 2013-10-05
      • 1970-01-01
      • 1970-01-01
      • 2013-05-10
      • 1970-01-01
      • 2018-01-18
      • 1970-01-01
      • 1970-01-01
      • 2015-07-17
      相关资源
      最近更新 更多