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 -
-
def triad(key,data):
-
try:
-
assert key in fields
-
except:
-
print "key %s not found" %key
-
raise ValueError,key
-
item = data[fields2[key]]
-
if key in noblanks:
-
item = item.replace(" ","")
-
if key.startswith("_"):
-
key = key.replace("_","") # hack for double column names
-
if isblank(item):
-
if defaults:
-
if key in defaults.keys():
-
return "<%s>"%key+defaults[key]+"</%s>"%key
-
else:
-
return "<%s>"%key+"(inconnu)</%s>"%key
-
else:
-
print line
-
raise BadLine
-
return "<%s>"%key+item+"</%s>"%key
-
-
defaults = {"NAS":"000000000","A_DateNaissance":"1/1/20","B1_NbJoursGarde":"0"}
-
noblanks = ["NAS","CodePostal"]
-
-
-
filename = "KR.tsv"
-
outname = "KR5.xml"
-
-
outfile = file(outname,"w")
-
-
data = [item.strip().split("\t") for item in file(filename).readlines()]
-
fields = data[0]
-
fields2 = dict([(item[1],item[0]) for item in enumerate(fields)])
-
-
# hack for double column names
-
fields2["_Prenom"]=4
-
fields2["_NomFamille"]=3
-
-
fields += "_Prenom _NomFamille".split()
-
# end hack for double column names
-
-
print fields2
-
-
outfile.write(prefix)
-
-
for line in data[1:]:
-
if not [item for item in line if (not isblank(item))]:
-
break # TSV file ends with many blank lines; exit and clean up on first one
-
try:
-
record = ["\n\n<R>"]
-
-
record.append(triad("Annee",line))
-
record.append(triad("NoReleve",line))
-
-
record.append("<Payeur>")
-
-
record.append(triad("NAS",line))
-
record.append(triad("_NomFamille",line))
-
record.append(triad("_Prenom",line))
-
-
record.append("<Adresse>")
-
record.append(triad("Ligne1",line))
-
record.append(triad("Ville",line))
-
record.append(triad("Province",line))
-
record.append(triad("CodePostal",line))
-
record.append("</Adresse>")
-
-
record.append("</Payeur>")
-
-
record.append("<Enfant>")
-
-
record.append(triad("NomFamille",line))
-
record.append(triad("Prenom",line))
-
record.append(triad("A_DateNaissance",line))
-
-
record.append("<CaseB>")
-
record.append(triad("B1_NbJoursGarde",line))
-
record.append("</CaseB>")
-
-
record.append("<Montants>")
-
record.append(triad("C_FraisPayes",line))
-
record.append("</Montants>")
-
-
record.append("</Enfant>")
-
-
record.append("</R>")
-
record = "\n".join(record)
-
outfile.write(record)
-
except BadLine:
-
pass
-
-
1 7999 bvdet 2,851
Expert Mod 2GB
To debug this, make a series of print statements prior to the line with the error. - print data
-
print fields2
-
print key
The error message is telling you that the list index is not valid for the list. Example: - >>> s = [0,1,2,3,4,5]
-
>>> s[6]
-
Traceback (most recent call last):
-
File "<interactive input>", line 1, in ?
-
IndexError: list index out of range
-
>>>
Sign in to post your reply or Sign up for a free account.
Similar topics
by: Andrew Dalke |
last post by:
Is there an author index for the new version of the
Python cookbook? As a contributor I got my comp version
delivered today and my ego wanted some gratification.
I couldn't find my entries.
...
|
by: Robert Brown |
last post by:
If I use _reverse_ wildcard search will it always result in a table
scan? Is it possible to get the DB (Oracle or SQL server) to use
indexes when doing reverse wildcard match?
let's say I have:...
|
by: ES Kim |
last post by:
"A Book on C" explains a technique to use an arbitrary array index range.
int* p = malloc(sizeof(int) * 10) - 1;
This way, the allocated array can be accessed with index range 1 ~ 10,
not 0 ~...
|
by: sam |
last post by:
hey everybody, this is my first time posting here. i'm pretty new to
python and programming in general (as you'll soon work out for
yourselves...)
i'm trying to code a version of a selection...
|
by: Russ |
last post by:
Every Python programmer gets this message occasionally:
IndexError: list index out of range
The message tells you where the error occurred, but it doesn't tell you
what the range and the...
|
by: muzu1232004 |
last post by:
If I have a table t1 with a column a as follows
A
1
1
1
1
1
1
1
|
by: ferlarroca |
last post by:
Hi, I am trying to copy a range below another one......
I have defined two ranges as follow:
Dim r1 As Range
Dim r2 As Range
Set r1 = Sheets("Old Index").Range("A9:A200, H9:H200, K9:K200")...
|
by: Steffen Stellwag |
last post by:
Truely is often better to scan a table in full passing by an index ,
but if you can force the optimizer to use an index via a hint for
testing and comparing the results.
But the index in the...
|
by: Bill Cunningham |
last post by:
I have create these 2 files. Called main.c and atr.c. They seem to work
pretty well. I just wanted to submit them to see what if any errors others
that know more might find. Thanks.
atr.c
...
|
by: evilbungle |
last post by:
Hi,
I know this is for Access but I was hoping someone may know about Excel and can help me.
I have created a Function in VBA for looking up a value in a grid it works fine but only if I place...
|
by: taylorcarr |
last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
|
by: aa123db |
last post by:
Variable and constants
Use var or let for variables and const fror constants.
Var foo ='bar';
Let foo ='bar';const baz ='bar';
Functions
function $name$ ($parameters$) {
}
...
|
by: ryjfgjl |
last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
|
by: ryjfgjl |
last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
| |