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))
打印class'dict'
我正在查看UnQlite文档,但发现不多。请帮助。
您已经有了字典,所以只需要搜索密钥
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')会得到值。