如何从UnQLite集合中提取字段值

2024-06-16 12:26:28 发布

您现在位置:Python中文网/ 问答频道 /正文

db = UnQLite('test.db')
data = db.collection('data')
print(data.fetch(0))

这张照片

{'id': b'abc', 'type': b'business', 'state': b'AZ', 'latitude': 33.3482589, 
 'name': b"ABC Restaurant", 'full_address': b'1835 E ABC Rd, Ste C109, Phoenix, AZ 85284',
 'categories': [b'Restaurants', b'Buffets', b'Italian'],
 'open': True, 'stars': 4, 'city': b'Phoenix', 'neighborhoods': [], 
 '__id': 0, 'review_count': 122, 'longitude': -111.9088346}

如何获取城市的“凤凰”值?

type(data.fetch(0))打印类“dict”

我正在查看UnQlite文档,没有找到多少。请帮忙


Tags: testiddbdatatypefetchbusiness照片
2条回答

您已经获得了dict,因此只需搜索密钥

x = {'id': b'abc', 'type': b'business', 'state': b'AZ', 'latitude': 33.3482589, 
 'name': b"ABC Restaurant", 'full_address': b'1835 E ABC Rd, Ste C109, Phoenix, AZ 85284',
 'categories': [b'Restaurants', b'Buffets', b'Italian'],
 'open': True, 'stars': 4, 'city': b'Phoenix', 'neighborhoods': [], 
 '__id': 0, 'review_count': 122, 'longitude': -111.9088346}
x['city']
#b'Phoenix'

这里Phoenix不是一个str对象,而是byte,所以如果您想将它作为字符串,可以使用decode来转换它

x['city'].decode()
#'Phoenix'

或者在您的情况下:

data.fetch(0)['city'].decode()

我猜到了。执行collection.fetch(0).get('city')将给出该值

相关问题 更多 >