1

I have a python code which iterates through all attribute fields and creates a map for each field automatically. However, in the code I have to specify each attribute manually which was fine so far. Now I have an attribute table with many fields and I need a way to do the same without specifying the attributes manually (as done with "Attrlist" in the code. Any suggestions?

This is the code I am using so far:

outPath = "D:\\Atlas\\test\MxdMaps\\Mxd_MapAutomation\\"   
# list of attributes with the same datatype
attrlist = ["EthnicGroups_totnumberoffarmhh_txt_WNHDeAA01", 
            "EthnicGroups_totnumberoffarmhh_txt_WNHDeAA02",    
            "EthnicGroups_totnumberoffarmhh_txt_WNHDeAA03"]
# layer which has to change the attribute field
featurelayer = arcpy.mapping.ListLayers(mxd, "EthnicGroups_totnroffarmhh")[0]
# text element that has to change (e.g. title...)
title = arcpy.mapping.ListLayoutElements(mxd, "TEXT_ELEMENT", "map_title")[0]
for attr in attrlist:
        #if featurelayer.symbologyType == "GRADUATED_COLORS":
                #print attr
                # change value field
                featurelayer.symbology.valueField = attr
                #featurelayer.symbology.addAllValues()
                featurelayer.symbology.reclassify()
                #print "Break values: %s" % featurelayer.symbology.classBreakValues
                # change tilte
                title.text = attr
                # update view
                arcpy.RefreshActiveView()
                arcpy.RefreshTOC()
                # export as jpeg
                outJPEG = os.path.join(outPath, attr + ".jpg")
                arcpy.mapping.ExportToJPEG(mxd, outJPEG, "PAGE_LAYOUT", 200, 200, 300, False, "24-BIT_TRUE_COLOR", 90, False)
                # export as AdobeIllustrator .ai
                #outAI = os.path.join(outPath, attr +  ".ai")
                #arcpy.mapping.ExportToAI(mxd, outAI, "PAGE_LAYOUT", 200, 200, 300, "BEST", "RGB", "VECTORIZE_BITMAP", False)
del mxd
2
  • 2
    arcpy.ListFields(featurelayer,"*","String") resources.arcgis.com/en/help/main/10.1/index.html#//… will give you all the string fields in a feature class - is that what you're after? Commented May 8, 2015 at 2:15
  • I think you should accept the answer to your first question and then edit out your second question because it should be researched/asked separately. The desirability of one question per question is mentioned in the Tour.
    – PolyGeo
    Commented May 8, 2015 at 11:06

1 Answer 1

4

arcpy.ListFields() will give you a list of objects. You just need to get the names from those objects.

field_names = [f.name for f in arcpy.ListFields(lyr)]

Another round about way of getting them would be via a search cursor but that also includes the shape field.

field_names = []
with arcpy.da.SearchCursor(lyr,"*") as cursor:
    field_names = cursor.fields
2
  • Thanks! I am almost there! Can you also tell me how I would change the code if I wanted to leave out like the first 20 fields and the last 2 fields from the List? Commented May 19, 2015 at 2:32
  • field_names[20:len(field_names)-2]
    – JasonT
    Commented May 19, 2015 at 2:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.