How to retrieve a single value from a TinyDB database?

丶灬走出姿态 提交于 2021-02-10 03:02:50

问题


I'm learning how to use TinyDB on Python and I have the basics working (add, remove, update etc.). But now I'm trying to retrieve specific values from the database. The code I'm using is in this method:

def showpassword():
    show = userdb.get(where("username") == txtname.get())
    txtshow.insert(0, show)

When I run the method, it displays the output as follows:

{'username': 'John', 'surname': 'Doe'}

How can I get it so that I can display only the user's name or surname and without any brackets?


回答1:


TinyDb stores its data as JSON which is represented in Python as a dictionary.

Change the line

txtshow.insert(0, show)

to

txtshow.insert(0, "{username} {surname}".format(**show)

Look here for a deeper dive in to string formatting.



来源:https://stackoverflow.com/questions/44239574/how-to-retrieve-a-single-value-from-a-tinydb-database

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!