【问题标题】:Listing the Document ID from TinyDB从 TinyDB 列出文档 ID
【发布时间】:2020-11-26 21:16:36
【问题描述】:

我正在尝试在 main.py 的第 40 行从这个 github (https://github.com/syntaxsmurf/todo/blob/main/main.py) 列出 db.json 的内容

特别是这一行

 for item in db:
        table.add_row( item["task"], item["completed_by"], item["status"]) #need to find the right command for pulling data out of tinyDB into these example strings

如您所见,我可以很好地拉出并列出我在 Fx 上使用 item["task]

定义名称的项目

如果你不想看 github,这里有一个来自 db.json 的示例条目。

{
    "_default": {
        "1": {
            "completed_by": "Today",
            "status": "Pending",
            "task": "Shopping"
        }
}

现在我缺少的是如何提取默认生成的 ID“1”并列出它?我想用它来让用户以后可以删除它。

谢谢你,我希望这个问题有意义!

【问题讨论】:

    标签: python json tinydb rich


    【解决方案1】:

    来自 reddit 用户 azzal07:item.doc_id 将是这个的正确实现。

    for item in db:
        table.add_row(str(item.doc_id), item["task"], item["completed_by"], item["status"])
    

    Str() 用于 Rich table 函数,如果它看起来是 int,则它不起作用。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-09-07
      • 2018-09-03
      相关资源
      最近更新 更多