【发布时间】:2021-12-01 17:52:48
【问题描述】:
我正在编写一个 python 程序来保存和检索云数据存储中的客户数据。我的实体如下所示:
entity.update({
'customerId': args['customerId'],
'name': args['name'],
'email': args['email'],
'city': args['city'],
'mobile': args['mobile']
})
datastore_client.put(entity)
我已成功保存数据。现在,我想从记录中检索一个随机的电子邮件 ID。我写了以下代码:
def get_customer():
query = datastore_client.query(kind='CustomerKind')
results = list(query.fetch())
chosen_customer = random.choice(results)
print(chosen_customer)
但是我只得到一个随机的电子邮件 ID,我得到的是这样的整行:
<Entity('CustomerKind', 6206716152643584) {'customerId': '103', 'city': 'bhubaneswar', 'name': 'Amit', 'email': 'amit@gmail.com', 'mobile': '7879546732'}>
谁能建议我怎样才能只得到 'email': 'amit@gmail.com' ?我是数据存储区的新手。
【问题讨论】:
-
你得到了整个条目。要从中提取,您也许可以使用
chosen_customer["email"]
标签: python google-cloud-platform google-cloud-datastore datastore