0

I have a program to make an address book, and I want to be able to confirm a change to a record before doing so--if I search for the last name "Peterson" and there are two entries, I can choose to change one, both, or neither. I'm trying to use basically the same code to either edit an existing line, or delete it from the program. I'm new to Python and this is my final project for a class, and I've spent like four days trying to figure out what isn't working. I've been looking through Stack Overflow and haven't found a satisfactory answer to my problem, and I am sure that's because I don't understand Python well enough. We're supposed to use the set up of creating and renaming a temp file, so while I know that's not the most efficient, it's what I'm supposed to do.

This is what I have:

import os
FIRSTNAME = 0
LASTNAME = 1
STREETADDRESS = 2
CITY = 3
STATE = 4
ZIP = 5
TELEPHONE1 = 6
TELEPHONE2 = 7

def modify():
    found = False
    search = input("Enter an item to search for: ")
    new = input("And what should we change it to? ")
    addressbook = open("addressbook.txt", "r")
    temp_file = open("temp.txt", "w")
    line = addressbook.readline()
    while line != "":
            line = line.rstrip("\n")
            lineInfo = line.split("|")
            if lineInfo[1] == search:
                    print("I found it!")
                    print(format(lineInfo[FIRSTNAME], '15s'),format(lineInfo[LASTNAME], '15s'),format(lineInfo[STREETADDRESS], '20s'),format(lineInfo[CITY], '10s'),
                          format(lineInfo[STATE], '5s'),format(lineInfo[ZIP], '10s'),format(lineInfo[TELEPHONE1], '15s')," ",format(lineInfo[TELEPHONE2], '10s'))
                    print()
                    delete = input("change this one? press y for yes.")
                    if delete == "y":
                            found = True
                            lineInfo[1] = new
                            temp_file.write(format(lineInfo[FIRSTNAME])+"|")
                            temp_file.write(format(lineInfo[LASTNAME])+"|")
                            temp_file.write(format(lineInfo[STREETADDRESS])+"|")
                            temp_file.write(format(lineInfo[CITY])+"|")
                            temp_file.write(format(lineInfo[STATE])+"|")
                            temp_file.write(format(lineInfo[ZIP])+"|")
                            temp_file.write(format(lineInfo[TELEPHONE1])+"|")
                            temp_file.write(format(lineInfo[TELEPHONE2])+"|")
                            temp_file.write("\n")
                    else:
                            temp_file.write(line)
                            temp_file.write("\n")
            else:
                    temp_file.write(line)
                    temp_file.write("\n")
            line = addressbook.readline()
    temp_file.close()
    os.rename("temp.txt","newaddress.txt")
    if found:
            print("File has been changed")
    else:
            print("File was not found")
 modify()

Presently when I run it, I get this:

Enter an item to search for: Peterson
And what should we change it to? Patterson
I found it!
Edward          Peterson        10 Grand Pl          
Kearny     NJ    90031      383-313-3003      xxx       

change this one? press y for yes.n
I found it!
James           Peterson        11 Grand Pl          
Kearny     NJ    90021      xxx               xxx       

change this one? press y for yes.y
Traceback (most recent call last):
File "C:\Users\kendr\Desktop\Address Book\Delete Address Book.py", line 53, in <module>
delete()
File "C:\Users\kendr\Desktop\Address Book\Delete Address Book.py", line 22, in delete
if lineInfo[1] == search:
IndexError: list index out of range

Honestly I'm at my wit's end with this assignment, so any and all help would make a huge difference. Thanks, K

Bips
  • 11
  • 2
  • Your file has some lines that don't have `|` in them. – Barmar Dec 09 '18 at 03:17
  • That's why I'm getting the error? – Bips Dec 09 '18 at 03:20
  • If there's no `|` then `line.split('|')` will return a list with only 1 element, so `lineInfo[1]` doesn't exist. – Barmar Dec 09 '18 at 03:21
  • The error is happening in the `delete()` function, but you posted `modify()`. – Barmar Dec 09 '18 at 03:22
  • BTW, don't use `lineInfo[1]` when you can write `lineInfo[LASTNAME]` instead. – Barmar Dec 09 '18 at 03:22
  • The delete and modify functions are the same--I renamed it for clarification here. I tried running the program with 3 entries, all of which have | between fields, and I still get the error. – Bips Dec 09 '18 at 03:30
  • And noted on the lineInfo[1] vs lineInfo[LASTNAME] thing! – Bips Dec 09 '18 at 03:31
  • Put `print(lineInfo)` before the `if` so you can see what the value is when it's getting the error. – Barmar Dec 09 '18 at 03:31
  • There were 2 blank lines at the end of the file, which is what it was getting caught on. Thank you so much--I'll definitely be using that trick again. – Bips Dec 09 '18 at 03:47
  • You should do `line = line.rstrip("\n")` before you test if the line is empty. – Barmar Dec 09 '18 at 03:54

1 Answers1

0

You need to move line = line.rstrip("\n") to before you check if the line is empty:

line = addressbook.readline().rstrip("\n")
while line != "":
    ...
    line = addressbook.readline().rstrip("\n")

Otherwise you'll read "\n" for the last line, and this will fail the test, so you'll go into the loop body and try to read process this empty line.

Barmar
  • 741,623
  • 53
  • 500
  • 612