简体   繁体   中英

I am wondering why my search phonebook function is not working correctly

I currently have a working phonebook with 4 options. The only thing is, the search option is not printing all of the matches. If I type a name and there is a match in the phonebook it will print that line into a text file (phone.txt02, which is blank). Then after all of the matches have been printed to the text file, my program reads the new text file and returns them in my preferred format.

The readFile function isn't working properly right now for the new text file. It works fine on phone.txt (the original text file) which contains the same information... Names and numbers separated by a comma. Because this works, I cannot figure out why the readFile function will not work for phone02.txt when the values are also name,number \n

def readFile1(filename):
    phonebook = []
    file = open(filename, "r")

    for aline in file:
        person = aline.split(",")

        if person[1][-1] == '\n' :
            pn = person.pop(1)
            person.append(pn[:-1])
            phonebook.append(person)
        elif person[1][-1] != '\n' :
            phonebook.append(person)


    file.close()

    return phonebook


def printEntries1(phonebook):
    readFile1("phone02.txt")
    print("Name                   Phone Number")
    print("-------------------  --------------")
    for i in range (len(phonebook)):
        person = phonebook[i]
        print(i,"{:<20s} {:>14s}".format(person[0],person[1]))
    print("-------------------  --------------")

def searchEntry():
    search = input("Type a name to search for")

    with open("phone.txt", "r") as file:
        lines = file.readlines()

        for line in lines:

            if search in line:
                outfile = open("phone02.txt", "a")
                outfile.write(line)
        phonebook = readFile1("phone02.txt")
        print(readFile1("phone02.txt"))
        printEntries1(phonebook)
        outfile = open("phone02.txt", "r+")
        outfile.truncate()

print(searchEntry())

I am not sure how to have the printEntries print all of the matches (name and number) from phone02.txt

Here is an example of the phone.txt file

Polly,549-5393
Bud Wieser,(213) 477-3928
Jack,277-4829
Mike Dunleavy,335-3453
Robert Darn,219-473-4373
Earl Lee,703-304-8393
Tim Bean,(612) 493-2629
Bud,(701) 487-8522 

If I were to input "Bud" it would print the 2 lines that contain bud to phone02.txt but not print them correctly.

It seems that (in this example) when the 2 lines containing Bud are put into phone02.txt, only the first line is printing

Name.                Number
-------------------  ------------------
0 Bud Wieser.       (218) 477-3928

I know this is a lot of information for most likely an easy fix, but I think it should help with the issue.

Thanks for any help.

You never closed the writer to phone02.txt . All new lines will be saved in the file only after you close the file writer or directly tell python to do save the changes to the file. Please try

if search in line:
    outfile = open("phone02.txt", "a")
    outfile.write(line)
    outfile.close()

Anyway, you open the file for reading many times -- please either do it once before the loop and close afterwards, or open and close the writer for each iteration.

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