I've written a python script using arcpy which outputs a polygon feature class into a File Geodatabase. I have added a function to export the attributes to a separate CSV file. I am using the code which I have found in this post which works perfectly. However, that code exports every column in the feature class. I only want to export the fields that do not have the following names: OBJECTID
, Shape
, or Shape_Length
.
My CSV file generates successfully, and it correctly does not include the OBJECTID
or Shape_Length
fields. However, the Shape
field is written to the file. An example value that is written to that field is:
<geoprocessing describe geometry object object at 0x28CB90A0>
I have added a line to print the field names as it iterates through them and surprisingly, Shape
is not printed. It's as if ArcGIS is hiding it or gives it a different name.
The code for my function is below:
def exportToTable():
"""
Exports the final outputs to a CSV File.
"""
# Create path to CSV File (note the varialbe outputPath is declared elsewhere).
CSVFile = outputPath+'\\FinalOutput.csv'
arcpy.AddMessage("Created CSV File: %s" %CSVFile)
# Get all fields in FinalOutput feature class and remove unwanted fields.
fields = arcpy.ListFields('FinalOutput')
for field in fields:
arcpy.AddMessage("Field.name is:"+field.name) #not printing 'Shape' field name
if field.name in (['OBJECTID', 'Shape', 'Shape_Length']):
fields.remove(field)
i = 1
f=open(CSVFile, 'w')
for field in fields:
#--write the wanted field names to the output file
if i < len(fields):
f.write('%s,' % field.name)
i += 1
else:
f.write('%s\n' % field.name)
# Use a search cursor to iterate through the rows of the table and write them to the CSV file.
rows = arcpy.SearchCursor('FinalOutput')
for row in rows:
i = 1
for field in fields:
if i < len(fields):
f.write('%s,' % row.getValue(field.name))
i += 1
else:
f.write('%s\n' % row.getValue(field.name))
del rows
f.close()
Does anyone know what's going on here?
I modified my code to follow the advice of @sgrieve and it was still writing the Shape
field. If I add a line to print the field names as it iterates through them, it lists all the fields except the Shape
field, yet it still writes to the CSV. It also added the X and Y coordinates of the polygon as two new columns and the columns no longer aligned with the column names.
I've modified the line where @sgrieve declares the fields to be the following:
fields = [f.name for f in arcpy.ListFields('FinalCadastre') if f.type <> 'Geometry']
The new code works fine, but I'm still unsure what the problem was. Does anyone know what was going on? What's the deal with the Shape
field?
Shape
field was being written to the file? While @sgrieve's code possibly improved my code, it did not solve the issue.