繁体   English   中英

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

[英]How to extract a field value from UnQLite collection

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))打印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 object 而是byte所以如果你想要它作为字符串你可以使用decode来转换它

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

或者在你的情况下:

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM