Select to view content in your preferred language

Confused with this error "list indices must be integers"

4008
21
Jump to solution
07-31-2012 08:34 AM
JonPedder
Deactivated User
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
21 Replies
JonPedder
Deactivated User
Yes the names populate correctly. The reason the list 'could' be empty is if there are no people in that Team yet. The team name woudl exist in the teams table by has no members at this time, so i need to account for that.

I'm very new to Python (like a week) and I'm unaware of dictionaries, which is why I didn't use them 🙂 Thanks for the other tips.

[ATTACH=CONFIG]16519[/ATTACH]

I've attached the run screen as a screenshot
0 Kudos
MathewCoyle
Honored Contributor
Yes the names populate correctly. The reason the list 'could' be empty is if there are no people in that Team yet. The team name woudl exist in the teams table by has no members at this time, so i need to account for that.

I'm very new to Python (like a week) and I'm unaware of dictionaries, which is why I didn't use them 🙂 Thanks for the other tips.

[ATTACH=CONFIG]16519[/ATTACH]

I've attached the run screen as a screenshot


Yes your list is empty so you will never enter this loop.

    for item in lMembers:
        wMembers = item
        arcpy.AddMessage(item)


Add this line before the loop to avoid undefined problems.
    item = None
0 Kudos
JonPedder
Deactivated User
Ah ha!

So if I'd like to increment wMembers to wMembers+item_Number and another to wMemberName+Item_value

    item = None
    for item in lMembers:
        wMembers+item_number = item
        wMewmberName+Value_of_lMember
        


so i'd end up with
wMembers1 containing value Jon P
wmembers2 containing value Arnold G
wMember3 containing value Pete H
etc

How would i manage that?

Thanks again
0 Kudos
MathewCoyle
Honored Contributor
Are you talking about creating dynamic variables for each item in your list? I don't think you really want to do that. You would want to use a dictionary to be able to look up when writing your output. Something like this.
members_dict = {}
for index,name in enumerate(lMembers,1):
    members_dict[index] = name

I'm still not clear on what you are doing with the index number though. Do you have a specific example of what your want your output string to be?
0 Kudos
JonPedder
Deactivated User
Sorry for being unclear.

As I mentioned, ultimately I'm writing values to an FDF file, which will be used in conjunction with a PDF template. The PDF contains fields for each Member, there can be up to 9 members. The fields are named Member1, Member2, etc. through Member9.

The line of code that writes the values out is this. Using the field name in the PDF/FDF, and the data from the script
txt.write("<</T(FDF Field Name)/V(" + str(arcpy variable name) + ")>>\n")
[\code]

I have potentially 9 team members, but not always. I may have none or some, not always 9.

I'm trying to write these values in a loop. So something like this

Loop through the members list
create a variable for each item in the list
populate that variable with the value from the list
write that variableto the FDF file using txt.write

so the loop would actually contain the txt.write
    for item in lMembers:

        FDF_Field_Name = FDF_Field_Name+item 
        FDF_Field_Name+item = lmembers+string_value

        txt.write("<</T(FDF_Field_Name)/V(" + str(FDF_Field_Name+item) + ")>>\n")
        arcpy.AddMessage(item)


something like that, probably clear as mud now ;)

Cheers
0 Kudos
MathewCoyle
Honored Contributor
Sorry for being unclear.

As I mentioned, ultimately I'm writing values to an FDF file, which will be used in conjunction with a PDF template. The PDF contains fields for each Member, there can be up to 9 members. The fields are named Member1, Member2, etc. through Member9.

The line of code that writes the values out is this. Using the field name in the PDF/FDF, and the data from the script
txt.write("<</T(FDF Field Name)/V(" + str(arcpy variable name) + ")>>\n")
[\code]

I have potentially 9 team members, but not always. I may have none or some, not always 9.

I'm trying to write these values in a loop. So something like this

Loop through the members list
create a variable for each item in the list
populate that variable with the value from the list
write that variableto the FDF file using txt.write

so the loop would actually contain the txt.write
    for item in lMembers:

        FDF_Field_Name = FDF_Field_Name+item 
        FDF_Field_Name+item = lmembers+string_value

        txt.write("<</T(FDF_Field_Name)/V(" + str(FDF_Field_Name+item) + ")>>\n")
        arcpy.AddMessage(item)


something like that, probably clear as mud now ;)

Cheers


So would this be an example of the strings you want to pass?
FDF_Field_Name = "Member1"
item = "Joe"
0 Kudos
JonPedder
Deactivated User
Yes, that's exactly it
0 Kudos
MathewCoyle
Honored 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.
0 Kudos
JonPedder
Deactivated User
Thanks so much for all your help today, I'll give this a shot shortly.

I'm not really sure exactly what's going on in this loop. Any chance you have time to explain this code? I'd rather learn why this works than blindly copy/paste to resolve my immediate need.

Thanks again

Jon
0 Kudos
JonPedder
Deactivated User
We're almost there, here's the output


<</T(Member1)/V(Member1Jon P)>>
<</T(Member2)/V(Member2Arnold G)>>
<</T(Member3)/V(Member3Art F)>>
<</T(Member4)/V(Member4Pete H)>>

I don't need the member1Jon P just Jon P in the second part.
0 Kudos