python - 无法从搁置中删除数据(自动化无聊的东西书)

标签 python python-3.x shelve

我正在做“用 Python 自动化无聊的东西”第 8 章的练习项目。我需要编写命令来从架子上删除一个关键字并删除整个关键字数据库。我不知道再尝试什么。我删除任何内容的尝试似乎都没有奏效。将不胜感激任何帮助。谢谢

#!/usr/bin/env python3
# mcb.pyw - Multiclipboard
# A program that can save and load pieces of text to the clipboard.
# Usage: python3 mcb.pyw save <keyword> - Saves clipboard to keyword.
#        python3 mcb.pyw <keyword> - Load keyword to clipboard.
#        python3 mcb.pyw list - Load all keywords to clipboard.

import shelve, xerox, sys

mcbShelf = shelve.open('mcb')

if len(sys.argv) == 3 and sys.argv[1].lower()== 'save':
    mcbShelf[sys.argv[2]] = xerox.paste()
elif len(sys.argv) == 2 and sys.argv[1].lower== 'clear':
    for i in list(mcbShelf.keys()):
        del mcbShelf[i]
elif len(sys.argv) == 2: 
    if sys.argv[1].lower() == 'list':
        xerox.copy(str(list(mcbShelf.keys())))
    elif sys.argv[1] in mcbShelf:
        xerox.copy(mcbShelf[sys.argv[1]])

#Extend the multiclipboard program in this so that it has a delet #<keyword> command line argument that will delete a keyword from the shelf.
elif len(sys.argv) == 3 and sys.argv[1].lower()== 'delete' and sys.argv[2].lower() in mcbShelf.keys():
    mcbShelf.pop[sys.argv[2]]

mcbShelf.close()

最佳答案

这是我对该项目的解决方案:

#! python3
# mcb.pyw - Saves and loads pieces of text to the clipboard.

""" Usage
    py.exe mcb.pyw save <keyword> - Saves clipboard to keyword.
    py.exe mcb.pyw <keyword> - Load keyword to clipboard.
    py.exe mcb.pyw list - Loads all keywords to clipboard.
    py.exe mcb.pyw delete <keyword> - Delete keyword from shelf
    py.exe mcb.pyw delete - Delete all keywords from shelf """


import sys, pyperclip, shelve

# Open new shelf file and save it inside a variable
mcb_shelf = shelve.open('mcb')

# Saves copied text in clipboard to keyword provided in argument to mcb_shelf
if len(sys.argv) == 3 and sys.argv[1].lower() == 'save':
    mcb_shelf[sys.argv[2]] = pyperclip.paste()

# Deletes keyword in argument if it exists in mcb_shelf
elif len(sys.argv) == 3 and sys.argv[1].lower() == 'delete':
    if sys.argv[2] in mcb_shelf:
        del mcb_shelf[sys.argv[2]]

# Checks if argument's length is 2
elif len(sys.argv) == 2:
    # Lists keywords if argument passed is list
    if sys.argv[1].lower() == 'list':
        pyperclip.copy(str(list(mcb_shelf.keys())))

    # Copies values in keyword passed in argument if it exists to the clipboard
    elif sys.argv[1] in mcb_shelf:
        pyperclip.copy(mcb_shelf[sys.argv[1]])

    # For loop that iterates through every keyword in mcb_shelf and deletes
    elif sys.argv[1] == 'delete':
        for keywords in list(mcb_shelf.keys()):
            del mcb_shelf[keywords]

mcb_shelf.close()

关于python - 无法从搁置中删除数据(自动化无聊的东西书),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47962161/

相关文章:

python - 在 Python 中使用数据帧上的时间戳值的 bool 过滤器

python - DatabaseError : Execution failed on sql, 无法将元组连接到字节

python - 计算快速傅里叶变换中的频率

python - 如何使用 Python 3 仅使用内置模块上传大文件?

python - 搁置代码给出 KeyError

python - 使用 channel 2 向一位用户发送通知

python - 为什么我的 Python Interactive shell 中的列表推导会附加一个 Nones 列表?

python 类只读属性(在日期时间)

python - 我无法让我的简单的 shelve python 脚本工作

python - 搁置模块有问题吗?