4

I frequently find myself in Python debug mode, trying to examine the fields of a layer or table.

arcpy.Describe(lyr).fields returns an array of <geoprocessing describe field object> which isn't much use.

About 10 times a day I find myself typing this:

flds = [] for fld in arcpy.Describe(lyr).fields: flds.append(fld.name)

Is there a faster way to obtain a readable listing of the layer or table's fields in arcpy?

2
  • 1
    all I can think of is ListFields: for f in arcpy.ListFields(lyr): print f.name, but basically that's the same... you could compact that down to one line, in the end it's all the same. Even in ArcObjects you get a fields object from a feature class then iterate through it for (x=0;x<fields.count;x++) { ...} – Michael Stimson May 26 '15 at 2:01
  • A bit better - at least I can keep that in Notepad and paste it rather than typing! – Stephen Lead May 26 '15 at 2:14
7

What you are doing works just fine. This is pretty much the same code but as a more elegant solution:

flds = [f.name for f in arcpy.ListFields(lyr)]
2
  • 2
    simplify it further: print [f.name...... ] , then save your common 'stuff' in a text file that you keep open when using ArcMap... now if only you could get rid of 'lyr' as something to change – user681 May 26 '15 at 2:19
  • 5
    yes, everyone should spend some time with python list comprehension. it's super powerful and will help in all kinds of unexpected ways. docs.python.org/2/tutorial/… – mr.adam May 26 '15 at 2:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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