Confused with this error "list indices must be integers"

2569
21
Jump to solution
07-31-2012 08:34 AM
JonPedder
Occasional Contributor II
Morning, I'm receiving this error when running a For loop "list indices must be integers". Could this be because my list is of stype string?

Here's the code. First I write values to list lMembers
rowMembers = arcpy.SearchCursor(fc6,qTeam)     lMembers = []     for z in rowMembers:         mName = z.getValue("Name")         lMembers.append(mName) 


Then I later try to write these values. Currently I'm just displaying them for debugging.
    for i in lMembers:         wMembers = lMembers         arcpy.AddMessage(lMembers)         arcpy.AddMessage(wMembers)


Not quite sure what I'm doing incorrectly

thanks in advance for you help

Jon
Tags (2)
0 Kudos
1 Solution

Accepted Solutions
MathewCoyle
Frequent Contributor
I'm not sure if you want the FDF_Field_Name variable inserted where you had it as a string before, but I did and you can change it back if that isn't what you are looking for.

    FDF_Field_Name = "Member"     for index,item in enumerate(lMembers,1):         name_field = "{0}{1}".format(FDF_Field_Name,index)         txt.write("<</T({0})/V({0}{1})>>\n".format(name_field,item))         arcpy.AddMessage(item)

Say this is your list.
lMembers = ["Joe","Sue","Anne"]

This will return for the first loop
name_field = "Member1" item = "Joe"

With that you can do what you want in terms of formatting for writing to your output.

View solution in original post

0 Kudos
21 Replies
MathewCoyle
Frequent Contributor
What are you trying to accomplish? You are referencing your list items as an index which isn't how it works. Your i variable holds your list value. Try this to see if it is what you want.
    for i in lMembers:
        arcpy.AddMessage(i)


Edit: If for whatever reason you need the index value as well, use the enumerate built-in.

for index,item in enumerate(lMembers):
    arcpy.AddMessage(index,item)
0 Kudos
BruceNielsen
Occasional Contributor III
The line
for i in lMembers:
assigns an item, not an index, from the lMembers list for each iteration. The next line:
wMembers = lMembers
is probably where your error is occuring. It is trying to extract the i'th value from the list, but since i is a string instead of an integer, the error occurs.

If you want to assign wMembers the value found in the lMembers list for each iteration of the for loop, you should change that line to
lMembers = i
0 Kudos
JonPedder
Occasional Contributor II
Here's a better explanation

The list lMembers contains values such as [jon, pete, paul, steve, etc]

when I iterate that list later I want to pull the values from this list and write them to an output, building an FDF file.

The code is attempting to do the following

loop for as many times as there are items in the list

for each list item, write that value to a output variable that can then be written out

0 Kudos
MathewCoyle
Frequent Contributor
Here's a better explanation

The list lMembers contains values such as [jon, pete, paul, steve, etc]

when I iterate that list later I want to pull the values from this list and write them to an output, building an FDF file.

The code is attempting to do the following

loop for as many times as there are items in the list

for each list item, write that value to a output variable that can then be written out



I'm not sure how to create an FDF file. All you need to do to get each item in the list is this.
for item in lMembers:
    #Write item to output
0 Kudos
JonPedder
Occasional Contributor II
I can do the FDF part (let me know if you're interested in how that works.

if I try the following, I receive this error. <type 'exceptions.NameError'>: name 'item' is not defined
for item in lMembers:
        wMembers = item
        arcpy.AddMessage(item)


lMembers is a list of peoples names, the number of folks vary. I need to write these names to variables to build my FDF file. So i actually need two variables creates, one for the variable name and one containing the persons name.

I thought I'd simplify my question by just asking how to pull the name value from the list.

In the end I'm looking to get something like this
variable item+numeric number for item (this would be the variable field placeholder, so 1, 2, 3, 4, etc)
variable name+ value for item (this is the string value from the lMembers, Jon, Steve, Amy, etc.)

the code will look somewhat like that, that writes the files
txt.write("<</T(item)/V(" + str(name) + ")>>\n")
0 Kudos
MathewCoyle
Frequent Contributor
You get that error if your list is empty. Try posting your entire code that may help track down the problem.
0 Kudos
JonPedder
Occasional Contributor II
Ok thanks, here goes. Note that I haven't written code to write out the lMembers info yet.


#arcpy.env.workspace = workspc
arcpy.env.overwriteOutput = "True"

from arcpy import env

# Pull data from Incident_Information FC
fc1="Incident_Information" 
rows = arcpy.SearchCursor(fc1)
row = rows.next()
arcpy.AddMessage(fc1)

while row:
    # Loop through rowns and pull incident name from Incident_Name field
    Incident_Name = row.getValue("Incident_Name")
    arcpy.AddMessage("while loop 1")
    row = rows.next()
del rows
del row

# Loops through the Assignmets table and read field values to vars
fc2="Assignments"

rows = arcpy.SearchCursor(fc2)
arcpy.AddMessage(fc2)
row = rows.next()

# Pull data from the Assignments feature class aVar from Assignments
while row:
    arcpy.AddMessage("Get Assignment Information")
    aAssign = row.getValue("Assignments.Assignment_Number")
    aDesc = row.getValue("Assignments.Description")
    aMiles = row.getValue("Assignments.Mileage")
    aInsertion = row.getValue("Assignments.Insertion")
    aStatus = row.getValue("Assignments.Status")
    aOpPeriod = row.getValue("Assignments.Period")
    aTeam = row.getValue("Assignments.Team_Name")

    # Pull data from Op Period FC using Period number
    arcpy.AddMessage("Matching Operation Period")
    fc4 = "Operation_Period"
    rowPeriod = arcpy.SearchCursor(fc4, "Period = " + str(aOpPeriod))
    # loops through operation periods and reads values into vars based on Op number pVar for Period
    for x in rowPeriod:
        pWeather = x.getValue("Weather")
        pSafety = x.getValue("Safety_Message")
        pFreqP = x.getValue("Primary_Comms")
        pFreqE = x.getValue("Emergency_Comms")
        pPlans = x.getValue("Planning_Chief")
    del x

    # Pull data from Teams FC using Assignment Number building quesry in var qTeam 
    arcpy.AddMessage("Matching Teams")
    fc5 = "Teams"
    qTeam = '"Team_Name"' + "='" + str(aTeam) + "'"
    arcpy.AddMessage(qTeam)
    rowPeriod = arcpy.SearchCursor(fc5,qTeam)
    # loops through Teams and reads values into vars based on Team name tVar for Team
    for y in rowPeriod:
        tType = y.getValue("Team_Type")
        tCallSign = y.getValue("radio_Call_Sign")
        tLeader = y.getValue("Leader")
    del y
    del rowPeriod
    
    # Pull data from Team Members FC using Team Name building query in mVar 
    arcpy.AddMessage("Matching Team Members")
    fc6 = "Team_Members"
    qTeam = '"Team_Name"' + "='" + str(aTeam) + "'"
    arcpy.AddMessage(qTeam)
    rowMembers = arcpy.SearchCursor(fc6,qTeam)
    lMembers = []
    # loops through Teams and reads values into list lMembers. List = lVar
    z = 0
    for z in rowMembers:
        mName = z.getValue("Name")
        lMembers.append(mName) 
        arcpy.AddMessage(mName)
    del z
    del rowMembers
    
    arcpy.AddMessage("Building Assignment Number " + str(aAssign))

    # Create new fdf file
    filename = output + "/" + str(aAssign) + ".fdf"

    txt= open (filename, "w")
    txt.write("%FDF-1.2\n")
    txt.write("%????\n")
    txt.write("1 0 obj<</FDF<</F(ICS-204.pdf)/Fields 2 0 R>>>>\n")
    txt.write("endobj\n")
    txt.write("2 0 obj[\n")

    txt.write ("\n")
    # Write field names to FDF file
    txt.write("<</T(incident_name)/V(" + str(Incident_Name) + ")>>\n")
    txt.write("<</T(assignment_number)/V(" + str(aAssign) + ")>>\n")
    txt.write("<</T(operational_period)/V(" + str(aOpPeriod) + ")>>\n")
    txt.write("<</T(asgn_description)/V(" + str(aDesc) + ")>>\n")
    txt.write("<</T(resource_type)/V(" + str(tType) + ")>>\n")
    txt.write("<</T(asgn.description)/V(" + str(aDesc) + ")>>\n")
    txt.write("<</T(.size_of_assignment)/V(" + str(aMiles) + ")>>\n")
    txt.write("<</T(transport_instructions)/V(" + str(aInsertion) + ")>>\n")
    txt.write("<</T(radio_call)/V(" + str(tCallSign) + ")>>\n")
    txt.write("<</T(freq_command)/V(" + str(pFreqP) + ")>>\n")
    txt.write("<</T(freq_command)/V(" + str(pFreqE) + ")>>\n")
    txt.write("<</T(prepared_by)/V(" + str(pPlans) + ")>>\n")
    txt.write("<</T(notes)/V(" + str(pWeather) + ")>>\n")

    # Write Team Members to FDF file from list lMembers
    for item in lMembers:
        wMembers = item
        arcpy.AddMessage(item)

    del item
        
    
    # Close and write FDF file
    txt.write("]\n")
    txt.write("endobj\n")
    txt.write("trailer\n")
    txt.write("<</Root 1 0 R>>\n")
    txt.write("%%EO\n")
    txt.close ()

    row = rows.next()
del rows
del row


I very much appreciate your help

Thanks

Jon
ChrisSnyder
Regular Contributor III
How about:

nameList = [r.Name for r in arcpy.SearchCursor(myFC)]
Then...

i = 0
for name in nameList:
    print "Item #" + str(i) + " = " + str(name)
    i = i + 1


There is a ".index()" method of a list, but it returns the index of the FIRST occurance of the specified item. For example:
test = ["john","sam","john"]
>>> test.index("john")
0

You probably want to be more explicit in this case, and include a counter instead of the list index (which makes the assumption that all the names will always be unique).
0 Kudos
MathewCoyle
Frequent Contributor
This is a case where dictionaries would have been superior. You have nested cursors which is frowned on in some circles, but as long as you know the cost isn't too high it shouldn't break anything. One note, when you are trying to reset a variable, I would avoid setting it to values like z = 0 or z = " ". Use z = None instead. These are mostly minor things and I don't see why your list would be empty.

Is this printing out all the expected names?
for z in rowMembers:
        mName = z.getValue("Name")
        lMembers.append(mName)
        arcpy.AddMessage(mName)
0 Kudos