'How can I extract a single field from TinyDB in python?
I'm new to python and TinyDB and cannot find the answer to this anywhere which tells me I'm overlooking something really simple.
With the following code:
from tinydb import TinyDB, Query
db = TinyDB('voltage_settings_db.json')
db.insert({'type': 'sample_times', 'times':60})
Setting = Query()
result = db.search(Setting.type == 'sample_times')
print(result)
Output: [{'type': 'sample_times', 'times': 60}]
How do I get a single field? In this case, I need to end up with an integer of 60.
I'm looking for something like the following:
time = result['times']
or
time = result.times
or
time = db.getfield('times')
UPDATE: I tried this at first but did not realize I had a typo and simply thought I was doing it wrong. So simple but nowhere do I find on the internet does it say how to get the value of an individual field. Really poor documentation. If there was documentation showing this I'd have looked closer at my code and noticed the typo. Now I know...
x = result[0]['times']
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
