Getting attribute table using Python in arcgis 10

6417
7
07-05-2011 08:01 PM
SreenivasaRaoPigili
Occasional Contributor
Hi,
   I am new to python scripting. Can anyone pls help me on the below query.
1.I have one MXD, which contains 10 shapefiles.
     ---- i am able to access these shapefiles using arcpy.mapping.listfeatures(mxd).
2.Based on the shapefile names, i have to access the different colmns in the attribute table.
    ---- Here i am unable to access the layer attribute table.
    ----- by using arcpy.listfields(shapefile), i am trying  to get the column values. But i am getting some object references.
Tags (2)
0 Kudos
7 Replies
JosephArmbruster
New Contributor III
I'm not sure if the behavior is the same in 10 but in 9.3 the fields returned by the listFields method are field objects.  You can access their name and type using the .Name and .Type members.  Here is a short example:

fields = gp.listFields(shapefile, "*", "ALL")
for field in fields:
   print field.Name, ' : ', field.Type
0 Kudos
DarrenWiens2
MVP Honored Contributor
If you want to access the actual cell values in the columns, use a cursor (like a SearchCursor).
0 Kudos
SreenivasaRaoPigili
Occasional Contributor
Hi,
   Thanks for giving reply. Now the issue got resolved.
Currently I am facing one more issue.
Actually I have two lists. let us say, listA = [1,2,3,4,3,2,1] and listB = [7,8,9,5,4,3,1]. Here have to get output like below.
in listA, we are getting value-1 in 2 indexes. I have to sum the values from listB in those positions.
Finally I have to output like below.
sum of 1's is ----7+1=8
sum of 2's is ---8+3=11
sum of 3's is----9+4=13
sum of 4's is----5

can you please help on this task.
0 Kudos
BruceNielsen
Occasional Contributor III
You can accomplish this by using a dictionary. Here's some code:
dict={}
for x in zip(listA,listB):
    if x[0] in dict:
        dict[x[0]] += x[1]
    else:
        dict[x[0]] = x[1]
newListA=dict.keys()
newListB=dict.values()
0 Kudos
DarrenWiens2
MVP Honored Contributor
There are many ways to do this (check here for an intro to lists), but I would start by making a loop to go through the list, a counter to keep track of the index position, and using the list.reverse method to flip the list. Add the current index value to the flipped index value.

Edit: Correct me if I'm wrong, but in your example you're adding the first to last value, second to second-last, and so on...
0 Kudos
JakeSkinner
Esri Esteemed Contributor
Are you trying to Sum the two lists together based on the index of each list?

Ex:

listA = [1,2,3,4,3,2,1]
listB = [7,8,9,5,4,3,1]

index[0] = 7 + 1 = 8
index[1] = 8 + 2 = 10
index[2] = 9 + 3 = 12

If you are, you can use the following code to do this:

import os

listA = [1,2,3,4,3,2,1]

listB = [7,8,9,5,4,3,1]

listC = []

x = 0

while x < len(listA):
    listC.append(listB + listA)
    x += 1
                 
print listC
0 Kudos
SreenivasaRaoPigili
Occasional Contributor
Hi All,
    Thanks for yours quick response.  Thanks Bruce Nielsen. yours approach is solving my problem.
0 Kudos