Table to csv

On the ESRI support forum someone asked how to convert an attribute table to a csv file. This can easily be done with a Python script. First we import arcgisscripting and the CursorIterator and initialize the geoprocessing object.
import arcgisscripting, string
from cursoriterator import CursorIterator

gp = arcgisscripting.create()
The next step is to initialize the different variables for this script. The workspace is the path to the directory, file geodatabase, personal geodatabase, sde,... where the script can find the table or feature class that you want to convert. The table is the name of the table or feature class you want to export. The outputpath is the path to file where you want your export to be written to. Be aware that the output directory should exist. If the file doesn't exist it will be created otherwise it will be overwritten. The csvseparator is the column separator for the output file. It could be anything you want. If you want to use tabs you should set this variable to '\t'. The last variable is ignorefields. If a field name in the table or featureclass is equal to a string in this list then this field won't be exported. This can be used for excluding the shape field, the objectid field or other fields you don't want to appear in the output file.
gp.workspace = r'' ## workspace of the table or feature class
table = '' ## table or feature class from wich the attributes should be exported
outputpath = r'' ## path to the file where the output should be written to
csvseparator = ',' ## column separator field
ignorefields = [] ##list with fields to ignore
Then we define a function wich purpose is to create a list of fieldnames that are to be exported to the output file. This is done by iterating over the fields cursor that can be accessed by the gp function listfields. The fields that are in the ignorefields list won't be returned.
def get_fieldnames(fields, ignorefields=[]):
   ignorefieldsupper = map(string.upper, ignorefields)
   fields = CursorIterator(fields)
   fields_output = []
   for field in fields:
       if not field.name.upper() in ignorefieldsupper:
           fields_output.append(field.name)
   return fields_output


fields = gp.listfields(table)
fieldnames = get_fieldnames(fields)
Now we are going to iterate over the rows in the table or featureclass and create a list with a string representation of each row. For each field we add the value of a field to a list. Then we concactenate this values with the chosen separator and add this to the output list.
rows = gp.searchcursor(table)
rows = CursorIterator(rows)

output = []
output.append(csvseparator.join(fieldnames))
for row in rows:
   outputrow = []
   for fieldname in fieldnames:
       outputrow.append(str(row.getvalue(fieldname)))

   outputrow = csvseparator.join(outputrow)
   output.append(outputrow)
Finally we write the outputlist to the defined outputpath.
f = open(outputpath, 'w')
f.write('\n'.join(output))
f.close()

The full code can be found on the arcscripts site.

7 comments:

Anonymous said...

I just copied this script to my python. It does not work giving error of import error. There is no cursoriterator.

Samuel Bosch said...

You can find the CursorIterator in the following post or you can download a working version of the table to csv script here

Ivan Phillipsen said...

Your script worked perfectly for. Thanks very much! I started to write my own script to do this, then thought, "I'll bet someone has already done this..."

Tyler K. said...

Hi! Do you have a version of this script for C#.Net? Trying to export the attribute fields of a feature class in a file geodatabase out to a csv file. Thanks!

Samuel Bosch said...

Don't have this lying around but shouldn't be hard to implement:
1) loop over fields in ITable and write the field names to a file
2) loop over rows in ITable.Rows and write the values to the same file

If you need more help then mail me and I'll see what I can do.

Anonymous said...

Thank you so much for posting! Quick question...
I am getting the following error:

f = open(outputpath, 'w')
Error Info:
: [Errno 13] Permission denied: 'C:\\Data\\NH_MailingList'

I am an administrator on my workstation and am also trying to write the csv to my harddrive on a folder I know I have permission to read/write. Any thoughts?

Anonymous said...

awesome.. script works well. thanks