简体   繁体   中英

I want to remove a line in a text file by asking the user to input an attribute in the line to delete it

So I have a txt file with ID's, student names and other attributes. I am asked to give the user the option to delete a studetn from the file by asking them to input their ID or their name only. Any ideas?

   ID    Name

eg ['102', 'Michael jackson', '3', '54', '30', '84']

def getlist():
    fp = open("student.txt", "r")
    list = fp.readlines()
    for i in range(len(list)):
        list[i] = list[i].split(";")
    return list

print("removing Students from the class based on")
        print("1-ID\t2-Student Name")
        fp=open("student.txt","r")
        
        list = getlist()
        c=int(input("Enter your choice:"))
        if(c==1):
            a=int(input("Enter the ID to remove:"))
            for i in range(1,len(list)):
                    if a==int(list[i][0]):
                        list.remove(list[i])
        else:
            b=input("Enter the Student name to remove")
            print("Records found under the name"+"("+b+")")
            for i in range(len(list)):
                if b==list[i][1]:
                    print(list[i],end=" ")
                    print("\n")

            ####this is for students with the same name
            z=int(input("Please select which record ID to remove:"))    
            
            for i in range(1,len(list)):
                #print(i)
                if z==int(list[i][0]):
                    list.remove(list[i])
                    break

Your project is almost done. You only need to create a function to save the file.

Comments:

  • Rename getlist to load_records . "get" is for something immediate; "load" is when you fetch something anew. Rename "list" to "records" (or "pupils", or "db"), as it's more descriptive (naming a variable "list" in Python is not a good idea anyway, as it's a name of a built-in function).

  • Rename a , b , z to name , id .

  • Have save_records too.

  • Try not to use the style for i in range(len(list)) if you can.

Eg, instead of:

list = fp.readlines()
for i in range(len(list)):
    list[i] = list[i].split(";")
return list

do:

list = []
for line in fp:
    list.append(line.split(';'))
return list

(More experienced programmers would write that function as:

def load_records():
    with open("student.txt") as f:
        return [ line.split(';') for line in f ]

)

  • Similarly, instead of:

     for i in range(len(list)): if b==list[i][1]: print(list[i],end=" ") print("\n")

Do:

        for rec in records:
            if b == rec[1]:
                print(rec, end=" ")
                print("\n")

(Experienced programmers would just write print([ rec for rec in records if rec[1] == b ]) .)

  • You duplicate the code that deletes a record. That's not good. Move the code that deletes a record (by ID) to a separate function.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM