如何在python上的记录中搜索文件?

时间:2016-05-06 00:09:36

标签: python

我正在编写一个程序,将学生写入文件,并在输入特定数据时进行搜索。但是我不确定我的脚本中的search_record方法是否正确。它有时可以返回正确的结果,但也可以在其他时候输入不起作用的不同数据。

TA =教师顾问

这是我目前的计划:

def read_record():
    in_file = open("student.txt", "r")

    stud_num = in_file.readline()

    while stud_num != "":
        last_name = in_file.readline
        first_name = in_file.readline
        TA = in_file.readline()

        stud_num = stud_num.rstrip("\n")
        last_name = last_name.rstrip("\n")
        first_name = first_name.rstrip("\n")
        TA = TA.rstrip("\n")

        print "Student number: ", stud_num
        print "Last name: ", last_name
        print "First name: ", first_name
        print "TA: ", TA
        print

        stud_num = in_file.readline()

    in_file.close()



def write_record():
    num_students = input("Enter number of students: ")

    out_file = open("student.txt" , "w")

    for counter in range (1, num_students + 1):
        stud_num = raw_input("Enter student number: ")
        last_name = raw_input("Enter last name: ")
        first_name = raw_input("Enter first name: ")
        TA = raw_input("Enter TA: ")

        out_file.write(stud_num +"\n")
        out_file.write(last_name +"\n")
        out_file.write(first_name +"\n")
        out_file.write(TA + "\n")

    out_file.close()

def menu():
    print "STUDENT MENU"
    print "============="
    print "A. Create student file"
    print "B. Display student file"
    print "C. Add student file"
    print "D. Search records"
    print
    choice = raw_input("Enter your choice: ")

    return choice

def add_record():
    out_file = open("student.txt", "a")

    another = "Y"

    while another == "Y":
        stud_num = raw_input("Enter student number: ")
        last_name = raw_input("Enter last name: ")
        first_name = raw_input("Enter first name: ")
        TA = raw_input("Enter TA: ")
        print

        out_file.write(stud_num + "\n")
        out_file.write(last_name + "\n")
        out_file.write(first_name + "\n")
        out_file.write(TA + "\n")

        another = raw_input("Do you want to enter another student (Y or N): ")

    out_file.close()

def search_record():
    in_file = open("student.txt", "r")

    found = False

    print "RECORD MENU"
    print "============="
    print "A. Search student number"
    print "B. Search student last name"
    print "C. Search student name"
    print "D. Search student TA"
    print

    choice = raw_input("Enter your choice: ")

    if choice == "A":
        search = raw_input("Enter a student number: ")
        stud_num = in_file.readline()

        while stud_num != "":
            last_name = in_file.readline()
            first_name = in_file.readline()
            TA = in_file.readline()

            stud_num = stud_num.rstrip("\n")
            last_name = last_name.rstrip("\n")
            first_name = first_name.rstrip("\n")
            TA = TA.rstrip("\n")

            if search == stud_num:
                print "Student number: ", stud_num
                print "Last name: ", last_name
                print "First name: ", first_name
                print "TA: ", TA
                print
                found = True

            stud_num = in_file.readline()


    elif choice == "B":
        search = raw_input("Enter a student last name: ")
        last_name = in_file.readline(2)

        while last_name != "":
            stud_num = in_file.readline()
            first_name = in_file.readline()
            TA = in_file.readline()


            stud_num = stud_num.rstrip("\n")
            last_name = last_name.rstrip("\n")
            first_name = first_name.rstrip("\n")
            TA = TA.rstrip("\n")

            if search == last_name:
                print "Student number: ", stud_num
                print "Last name: ", last_name
                print "First name: ", first_name
                print "TA: ", TA
                print
                found = True

            last_name = in_file.readline()

    elif choice == "C":
        search = raw_input("Enter a student name: ")
        first_name = in_file.readline(3)

        while first_name != "":
            stud_num = in_file.readline()
            last_name = in_file.readline()
            TA = in_file.readline()           

            stud_num = stud_num.rstrip("\n")
            last_name = last_name.rstrip("\n")
            first_name = first_name.rstrip("\n")
            TA = TA.rstrip("\n")

            first_name = in_file.readline()

            if search == first_name:
                print "Student number: ", stud_num
                print "Last name: ", last_name
                print "First name: ", first_name
                print "TA: ", TA
                print
                found = True
            first_name = in_file.readline()


    elif choice == "D":
        search = raw_input("Enter a TA: ")
        TA = in_file.readline(4)

        while TA != "":
            stud_num = in_file.readline()
            last_name = in_file.readline()
            first_name = in_file.readline()


            stud_num = stud_num.rstrip("\n")
            last_name = last_name.rstrip("\n")
            first_name = first_name.rstrip("\n")
            TA = TA.rstrip("\n")

            if search == TA:
                print "Student number: ", stud_num
                print "Last name: ", last_name
                print "First name: ", first_name
                print "TA: ", TA
                print
                found = True

            TA = in_file.readline()


    else:
        print "Invalid entry"


    in_file.close()

    if not found:
        print "Student record not found"


def main():
    choice = menu()

    if choice == "A":
        write_record()
    elif choice == "B":
        read_record()
    elif choice == "C":
        add_record
    elif choice == "D":
        search_record()
    else:
        print "error"


main()

我创建的学生文件是

12345
Potter
Harry
Wong
6789
Turner
Timmy
Mack

1 个答案:

答案 0 :(得分:0)

首先,我想提一下你有很多剪切和粘贴的代码。

然后使搜索功能正常工作。 您需要运行与搜索学生编号时几乎相同的代码。唯一的区别是您要求选择不同的提示B按学生姓氏搜索要求姓氏而不是学号。 而不是比较search == stud_num: 你比较search == last_name: ......这应该是唯一的区别。 循环和读取数据的方式应与用户输入选择A ...

时相同

但是它没有用,因为你首先尝试读取last_name,并且你正在调用readline()但是使用一个指定要读取的字节数的数字。这就是它失败的原因。