1
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}

如何获取 City 的值“Phoenix”?

type(data.fetch(0))打印类 'dict'

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

4

2 回答 2

2

你已经得到了一个字典,所以你只需要搜索密钥

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()
于 2019-11-14T04:52:24.490 回答
0

我想通了。做一个 collection.fetch(0).get('city') 给出值。

于 2019-11-14T04:54:13.323 回答