更改记录中的字段会产生IndexError

时间:2018-12-09 03:14:28

标签: python python-3.x addressbook

我有一个用于制作地址簿的程序,并且我希望能够在确认之前对记录进行更改-如果我搜索姓氏“ Peterson”并且有两个条目,则可以选择更改一个,两个或两个都不更改。我正在尝试使用基本相同的代码来编辑现有行,或将其从程序中删除。 我是Python的新手,这是我上一堂课的最后一个项目,我花了整整四天的时间来弄清楚什么是行不通的。我一直在研究Stack Overflow,但没有找到令人满意的答案,这是因为我对Python的了解不够。我们应该使用创建和重命名临时文件的设置,因此虽然我知道这不是最有效的,但我应该这样做。

这就是我所拥有的:

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()

当我运行它时,我得到了:

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

老实说,我的这项工作已经走到了尽头,因此任何帮助都将带来巨大的改变。 谢谢, K

1 个答案:

答案 0 :(得分:0)

您需要先将line = line.rstrip("\n")移至,然后再检查行是否为空:

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

否则,您将在最后一行读取"\n",这将使测试失败,因此您将进入循环正文并尝试读取处理此空行。