[GIS] Exporting only certain columns to CSV file in ArcGIS for Desktop

arcgis-10.1arcpycsvfields-attributesgeometry

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?

Best Answer

I have simplified your code and corrected the error by using the da module introduced in 10.1. It greatly streamlines the reading of data using cursors, and used in conjunction with the with command this code should be more stable than if it used the the older method of file access.

It works by making a list of all the fields and then removing the fields you do not want from the list. This could be done within the list comprehension, but it would be pretty messy and un-pythonic. Once the list of desired fields has been created it is used with the da module to read all of the data in these fields into the cursor. This can then be looped through and written to the file using another list comprehension to join all the fields. This has the benefit of working for any number of fields greater than 0.

import arcpy

fc = 'C:\\antenna_shp\\cables.shp'
CSVFile = 'C:\\antenna_shp\\FinalOutput.csv'

fields = [f.name for f in arcpy.ListFields(fc)]

for i,f in enumerate(fields):
    if f == 'Shape' or f == 'Shape_Length' or f == 'OBJECTID':
        del fields[i]

with open(CSVFile, 'w') as f:
    f.write(','.join(fields)+'\n') #csv headers
    with arcpy.da.SearchCursor(fc, fields) as cursor:
        for row in cursor:
            f.write(','.join([str(r) for r in row])+'\n')
Related Question