【问题标题】:Comparing Values of two lists and Printing to a file比较两个列表的值并打印到文件
【发布时间】:2014-03-21 05:13:54
【问题描述】:

我有一个程序应该将列表的内容与 tinter 树视图返回的值进行比较,如果值不匹配,则将列表的元素写入文件。这个想法是允许用户删除树中的一个元素(通过从我试图写入的同一个文件中读取来填充)。 这是代码:

    selected_book = info_box.focus()
    del_book = info_box.item(selected_book, 'values')
    title_file_clear = open("titles", 'w')
    author_file_clear = open("authors", 'w')
    title_file_clear.close()
    author_file_clear.close()
    title_file_3 = open("titles", "a")
    author_file_3 = open("authors", "a")
    for i in range(0,len(titles)):
        if titles[i] == del_book[0] is False:
            print(titles[i], file=title_file_3)
    for i in range(0,len(authors)):
        if authors[i] == del_book[1] is False:
            print(authors[i], file=author_file_3)
    title_file_3.close()
    author_file_3.close()

但它似乎所做的只是将文件清空。 (我知道这可能不是最有效的代码,但我已经调整了一段时间以尝试让它工作)

【问题讨论】:

  • 为什么不用if titles[i] != del_book[0] 而不是is False
  • 您打开文件,但从不读取文件。你确定这是工作代码吗? titles 是什么? authors 是什么?一个建议:执行程序,并确保没有语法错误,以便我们帮助您解决逻辑错误。
  • print 就是这样工作的。
  • @karthikr 是的,这只是代码中的一个模块,其余的工作正常。我只是假设没有必要上传所有 190 多行。

标签: python tkinter treeview


【解决方案1】:

使用if titles[i] != del_book[0]: 而不是if titles[i] == del_book[0] is False:。 要追加文件而不是编写新行,请查看 at this question

【讨论】:

    猜你喜欢
    • 2020-12-13
    • 1970-01-01
    • 2020-12-29
    • 2023-02-25
    • 2020-10-16
    • 2016-10-23
    • 1970-01-01
    • 1970-01-01
    • 2013-04-25
    相关资源
    最近更新 更多