我有一个用于管理员工数据库的脚本。添加员工记录并将数据附加到CSV文件后,我关闭程序。从头开始,我尝试导入CSV并返回错误“ IndexError:列表索引超出范围”。 附加csv并关闭程序后,然后使用notpad ++打开csv。在文件中,添加了另外两行。 使用try-除了块,如何从CSV文件中删除行? 在add_employee函数中,谁能看到为什么要添加另外两行?
def import_employees():
with open('Employee_database.csv', 'r') as csvfile: #Opens the student answer file, and closes once finished
employee_reader = csv.reader(csvfile)
first_row = True
for row in employee_reader:
#Skip the first row with column names
if first_row:
first_row = False
continue
try:
fullName = row[0] + ' ' + row[1] #Creates full name for dictionary key
employeeList[fullName] = row
except IndexError:
del
def add_employee(): #CPT 200: Functionality 1 part 1
employeeFName = input("Please Emter the Employee #%d's First Name:" % num)
employeeLName = input("Please Enter the Employee #%d's Last Name:" % num)
employeeSSN = input("Please Enter %s %s's SSN (123456789):" % (employeeFName, employeeLName))
employeePhone = input("Please Enter %s %s's Phone Number:" % (employeeFName, employeeLName))
employeeEmail = input("Please Enter %s %s's Email Address:" % (employeeFName, employeeLName))
employeeSalary = input("Please Enter %s %s's Salary:" % (employeeFName, employeeLName))
employeeFull = employeeFName + ' ' + employeeLName
print(" ")
employeeInfo = [employeeFName.capitalize(), employeeLName.capitalize(), employeeSSN, employeePhone, employeeEmail, employeeSalary]
if (employeeFull and employeeSSN) not in employeeList.keys():
with open('Employee_database.csv', 'a+') as employeeCSV:
employee_writer = csv.writer(employeeCSV)
employee_writer.writerow(employeeInfo)
else:
print('Employee is already in the database, please try again.')
return employeeFull.upper(), employeeInfo