Python-如何为用户输入创建删除功能?

我一直在尝试创建一个程序,该程序允许用户查看文本文件的内容并删除单个条目块的部分或全部。

下面是文本文件内容的示例:

Special Type A Sunflower
2016-10-12 18:10:40
Asteraceae
Ingredient in Sunflower Oil
Brought to North America by Europeans
Requires fertile and moist soil
Full sun

Pine Tree
2018-12-15 13:30:45
Pinaceae
Evergreen
Tall and long-lived
Temperate climate

Tropical Sealion
2019-01-20 12:10:05
Otariidae
Found in zoos
Likes fish
Likes balls
Likes zookeepers

Big Honey Badger
2015-06-06 10:10:25
Mustelidae
Eats anything
King of the desert

因此,输入块是指所有没有水平空格的行。

目前,我的进度是:

import time
import os
global o
global dataset
global database
from datetime import datetime

MyFilePath = os.getcwd() 
ActualFile = "creatures.txt"
FinalFilePath = os.path.join(MyFilePath,ActualFile) 

def get_dataset():
    database = []
    shown_info = []
    with open(FinalFilePath,"r") as textfile:  
        sections = textfile.read().split("\n\n")
        for section in sections:                 
            lines = section.split("\n")      
            database.append({                
              "Name": lines[0],"Date": lines[1],"Information": lines[2:]          
            })
    return database

def delete_creature():
    dataset = get_dataset()
    
    delete_question = str(input("Would you like to 1) delete a creature or 2) only some of its information from the dataset or 3) return to main page? Enter 1,2 or 3: "))
    
    if delete_question == "1":
        delete_answer = str(input("Enter the name of the creature: "))
        for line in textfile:
            if delete_answer in line:
                line.clear()
            
    elif delete_question == "2":
        delete_answer = str(input("Enter the relevant information of the creature: "))
        for line in textfile:
            if delete_answer in line:
                line.clear()
        
    elif delete_question == "3":
        break
        
    else:
        raise ValueError
    except ValueError:
        print("\nPlease try again! Your entry is invalid!")
        
while True:
    try:
        option = str(input("\nGood day,This is a program to save and view creature details.\n" +
                       "1) View all creatures.\n" +
                       "2) Delete a creature.\n" +
                       "3) Close the program.\n" +
                       "Please select from the above options: "))
        
        if option == "1":
            view_all()
            
        elif option == "2":
            delete()
                
        elif option == "3":
            break
            
        else:
            print("\nPlease input one of the options 1,2 or 3.")
        
    except:
        break

delete_function()用于通过以下方式删除生物:

  1. 名称,它将删除与该名称关联的整个文本块
  2. 信息,仅删除信息行

但是,我似乎无法使delete_creature()函数正常工作,而且我不确定如何使其正常工作。

有人知道如何使它工作吗?

非常感谢!

相关文章

Python中的函数(二) 在上一篇文章中提到了Python中函数的定...
Python中的字符串 可能大多数人在学习C语言的时候,最先接触...
Python 面向对象编程(一) 虽然Python是解释性语言,但是它...
Python面向对象编程(二) 在前面一篇文章中谈到了类的基本定...
Python中的函数(一) 接触过C语言的朋友对函数这个词肯定非...
在windows下如何快速搭建web.py开发框架 用Python进行web开发...