【发布时间】:2014-01-01 20:12:26
【问题描述】:
datos = {"nombre": "Juan","apellido1": "Perez",
"apellido2": "Palma","sexo": "M","codigo":"30","ingreso": "2009"}
def generar_rol(datos):
from definiciones import digito_verificador
archivo= open('alumnos.dat')
zzz=0
nombret=datos['nombre']+datos['apellido1']+datos['apellido2']
for linea in archivo:
zzz+=1
todo=linea.strip().split()
print todo[2].split('|') #<----HERE (line 24)
nombre = todo[2].split('|')
nombretotal=''.join(nombre)
if nombret==nombretotal:
break
zzz= str(zzz)
Z = ["0", "0", "0"]
if len(zzz) >= 1 :
Z[-1] = zzz[-1]
if len(zzz) >= 2 :
Z[-2] = zzz[-2]
if len(zzz) >= 3 :
Z[-3] = zzz[-3]
zzz = "".join(Z)
anno=''
for i in datos:
if int(datos['ingreso'])<2010:
anno=datos['ingreso'][0]+datos['ingreso'][-1]
if int(datos['ingreso'])>=2010:
anno=datos['ingreso']
rol=anno+datos['codigo']+str(zzz)
rol= rol+'-'+digito_verificador(rol)
archivo.close()
return rol
generar_rol(datos)
我有这个错误...
['JUAN', 'PEREZ', 'PALMA']
Traceback (most recent call last):
File "C:\Users\pinky\Downloads\TAREA 3\TAREA 3\Tarea 3\funciones.py", line 50, in <module>
generar_rol(datos)
File "C:\Users\pinky\Downloads\TAREA 3\TAREA 3\Tarea 3\funciones.py", line 24, in generar_rol
print todo[2].split('|') #<----HERE
IndexError: list index out of range
【问题讨论】:
-
linea和todo的内容是什么? -
在我看来
todo[2]应该是todo[1].. 超出范围的索引告诉您索引2超出范围。 -
linea 是:“Juan.Perez.51@sansano.usm.cl 201341040-5 JUAN|PEREZ|PALMA M;30;2015;jualma5”和待办事项:['Juan.Perez.51@ sansano.usm.cl', '201341040-5', 'JUAN|PEREZ|PALMA', 'M;30;2015;jualma5']
-
但根据字符串 todo[1] 是 ['201341040-5']
-
linea是文件的第一行吗?还是只有一个?可能函数的最后一个空行有问题。