By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,035 Members | 1,984 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,035 IT Pros & Developers. It's quick & easy.

How to fix "list index out of range" error?

P: 2
This is my first program of any sort I am getting this error

File "karenrosen5.py", line 162, in <module>
record.append(triad("NoReleve",line))
File "karenrosen5.py", line 115, in triad
item = data[fields2[key]]
IndexError: list index out of range

The code in question is
Expand|Select|Wrap|Line Numbers
  1.  
  2. def triad(key,data):
  3.     try:
  4.         assert key in fields
  5.     except:
  6.         print "key %s not found" %key
  7.         raise ValueError,key
  8.     item = data[fields2[key]]
  9.     if key in noblanks:
  10.         item = item.replace(" ","")
  11.     if key.startswith("_"):
  12.         key = key.replace("_","")    # hack for double column names
  13.     if isblank(item):
  14.         if defaults:
  15.             if key in defaults.keys():
  16.                 return  "<%s>"%key+defaults[key]+"</%s>"%key
  17.             else:
  18.                 return  "<%s>"%key+"(inconnu)</%s>"%key
  19.         else:
  20.             print line
  21.             raise BadLine
  22.     return "<%s>"%key+item+"</%s>"%key
  23.  
  24. defaults = {"NAS":"000000000","A_DateNaissance":"1/1/20","B1_NbJoursGarde":"0"}
  25. noblanks = ["NAS","CodePostal"]
  26.  
  27.  
  28. filename = "KR.tsv"
  29. outname = "KR5.xml"
  30.  
  31. outfile = file(outname,"w")
  32.  
  33. data = [item.strip().split("\t") for item in file(filename).readlines()]
  34. fields = data[0]
  35. fields2 = dict([(item[1],item[0]) for item in enumerate(fields)])
  36.  
  37. # hack for double column names
  38. fields2["_Prenom"]=4
  39. fields2["_NomFamille"]=3
  40.  
  41. fields += "_Prenom _NomFamille".split()
  42. # end hack for double column names
  43.  
  44. print fields2
  45.  
  46. outfile.write(prefix)
  47.  
  48. for line in data[1:]:
  49.     if not [item for item in line if (not isblank(item))]:
  50.         break     # TSV file ends with many blank lines; exit and clean up on first one
  51.     try:
  52.         record = ["\n\n<R>"]
  53.  
  54.         record.append(triad("Annee",line))
  55.         record.append(triad("NoReleve",line))
  56.  
  57.         record.append("<Payeur>")
  58.  
  59.         record.append(triad("NAS",line))
  60.         record.append(triad("_NomFamille",line))
  61.         record.append(triad("_Prenom",line))
  62.  
  63.         record.append("<Adresse>")
  64.         record.append(triad("Ligne1",line))
  65.         record.append(triad("Ville",line))
  66.         record.append(triad("Province",line))        
  67.         record.append(triad("CodePostal",line))
  68.         record.append("</Adresse>")
  69.  
  70.         record.append("</Payeur>")
  71.  
  72.         record.append("<Enfant>")
  73.  
  74.         record.append(triad("NomFamille",line))
  75.         record.append(triad("Prenom",line))
  76.         record.append(triad("A_DateNaissance",line))
  77.  
  78.         record.append("<CaseB>")
  79.         record.append(triad("B1_NbJoursGarde",line))
  80.         record.append("</CaseB>")
  81.  
  82.         record.append("<Montants>")
  83.         record.append(triad("C_FraisPayes",line))
  84.         record.append("</Montants>")
  85.  
  86.         record.append("</Enfant>")
  87.  
  88.         record.append("</R>") 
  89.         record = "\n".join(record)
  90.         outfile.write(record)
  91.     except BadLine:
  92.         pass
  93.  
  94.  
Feb 2 '11 #1
Share this Question
Share on Google+
1 Reply


bvdet
Expert Mod 2.5K+
P: 2,851
To debug this, make a series of print statements prior to the line with the error.
Expand|Select|Wrap|Line Numbers
  1. print data
  2. print fields2
  3. print key
The error message is telling you that the list index is not valid for the list. Example:
Expand|Select|Wrap|Line Numbers
  1. >>> s = [0,1,2,3,4,5]
  2. >>> s[6]
  3. Traceback (most recent call last):
  4.   File "<interactive input>", line 1, in ?
  5. IndexError: list index out of range
  6. >>> 
Feb 2 '11 #2

Post your reply

Sign in to post your reply or Sign up for a free account.