5

I want to use arcpy/python to print out a list of all the field name aliases for a specified shapefile. So far I have been able to hack a script I found to list all the field name aliases associated with every layer file on my map, but I wish for it to reference one specific shapefile. Here's what I have so far:

Code:

import arcpy
mxd = arcpy.mapping.MapDocument("CURRENT")
for lyr in arcpy.mapping.ListLayers(mxd):
 desc = arcpy.Describe(lyr)
 print lyr.name
 fields = desc.fields
 for field in fields:
 print field.aliasName

The above returns a list of all field alias names for ALL of the layers in the map. I want to return just a list for ONE shapefile.

How would I go about referencing just one shapefile/layer in a map document?

PolyGeo
65.5k29 gold badges115 silver badges349 bronze badges
asked Dec 16, 2012 at 0:45

1 Answer 1

9

Instead of iterating over the whole list returned by ListLayers, pass in the wildcard parameter using your layer name and just process the first item, e.g.

import arcpy
mxd = arcpy.mapping.MapDocument("CURRENT")
lyr = arcpy.mapping.ListLayers(mxd, "layer_name")[0]
desc = arcpy.Describe(lyr)
print lyr.name
fields = desc.fields
for field in fields:
 print field.aliasName
answered Dec 16, 2012 at 1:57
7
  • excellent. I was looking at the wildcard parameter but wasn't quite sure as to how to use it properly. many thanks! Commented Dec 16, 2012 at 3:09
  • Just tried it. It executes perfectly, but it returns a list of the field names--not the alias name of the field. Interesting... Commented Dec 16, 2012 at 3:14
  • Are you sure they have aliases? Commented Dec 16, 2012 at 3:20
  • Yes. The reason I know this is because if I run the code as I had originally, it generates all the field name aliases for that layer (the one I want) plus all the other layers that I don't want. I just want to print out the alias names for the specific layer. Commented Dec 16, 2012 at 3:26
  • Well there is no functional difference between the code you posted and the code I posted except which layer(s) are being processed, so I would check to see that you haven't overlooked something (are there two similarly named layers, for example?). Commented Dec 16, 2012 at 3:42

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.