2

I have a feature class with several fields in the attribute table, and I want to export a few but not all of those fields out to a tab delimited text file. The code below sort of works, half of the fields in the uneeded_fields variable are dropped, but three (SHAPE, long, and utm_y) still end up in my text file. I'm not sure why some of the fields are deleted while the other are passed on to the search cursor. I have tried copying the names from the field set up in Catalog and pasting them into my script so it isn't a typo issue. I'm not sure what I'm missing here.

As a side note, will the SHAPE field always be the first field returned unless specified otherwise? If so del fields[0] can take care of that, but the other will still be a problem.

fields = [f.name for f in arcpy.ListFields(fc)]
uneeded_fields = ('SHAPE', 'OBJECTID', 'lat', 'long','utm_x', 'utm_y')
for i,f in enumerate(fields):
 if f in uneeded_fields:
 del fields[i]
with open(outfile, 'w') as f:
 f.write('\t'.join(fields)+'\n') #column headers
 with arcpy.da.SearchCursor(fc, fields) as cursor:
 for row in cursor:
 f.write('\t'.join([str(r) for r in row])+'\n')
asked Jan 3, 2014 at 22:08

1 Answer 1

7

Try something like this:

uneeded_fields = ['SHAPE', 'OBJECTID', 'lat', 'long','utm_x', 'utm_y']
fields = [f.name for f in arcpy.ListFields(fc) if f.name not in uneeded_fields]
with open(....
answered Jan 3, 2014 at 22:18
0

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.