gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-04 01:59:56 25 4
gpt4 key购买 nike

我正在做“用 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com