簡體   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