首页 > 解决方案 > 更改记录中的字段会产生 IndexError

问题描述

我有一个制作地址簿的程序,我希望在这样做之前能够确认对记录的更改——如果我搜索姓氏“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

老实说,我对这项任务束手无策,所以任何和所有的帮助都会产生巨大的影响。谢谢,克

标签: pythonpython-3.xaddressbook

解决方案


line = line.rstrip("\n")在检查该行是否为空之前,您需要移动到:

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

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


推荐阅读