# Python script to export .csv tables from GDB

I am very new to Python programming and have been tasked with writing a program to export a csv file from a file geodatabase feature class. The csv should contain only certain fields and the records exported should be based on last edit date. In other words the csv file will be created on a daily bases containing only the last features added based on "last edit date field".

I have this so far:

import arcpy
import os
import csv
import domainvalues

def export_to_csv(dataset, output, dialect):
"""Output the data to a CSV file"""
# create the output writer
out_writer = csv.writer(open(output, 'wb'), dialect=dialect)
# return the list of field names and field values
header, rows = domainvalues.header_and_iterator(dataset)

# write the field names and values to the csv file
out_writer.writerow(map(domainvalues._encodeHeader, header))
for row in rows:
out_writer.writerow(map(domainvalues._encode, row))

if __name__ == "__main__":
# Get parameters
dataset_name = arcpy.GetParameterAsText(0)
output_file = arcpy.GetParameterAsText(1)
delim = arcpy.GetParameterAsText(2).lower()
dialect = 'excel'
if delim == 'comma':
pass
else:
dialect = 'excel-tab'
try:
export_to_csv(dataset_name, output_file, dialect)
except Exception as err:
arcpy.AddError('Error: {0}'.format(err))


However its exporting everything.

-
How do you post what you have so far? New to stackexchange also –  user35267 Jul 31 '14 at 14:07
I think what Curlew means is that you should put some code in your question (edit your question) so it shows that you have tried to do this and then we can help you figure out where you may be going wrong (so it doesn't appear that you are just attempting to get the community to write a script for you). –  jbchurchill Jul 31 '14 at 14:16

## 2 Answers

A simpler solution would be to convert to dbf file format, in which case you you can use the out-of-the-box Table to Table (Conversion). This tool also allows you the freedom to select which fields to include as FieldMappings as well as directly outputting a .dbf file.

import arcpy

fc = r'C:\path\to\your\fc'
outws = r'C:\temp'

arcpy.TableToTable_conversion (fc, outws, 'outFile.dbf')

-
-1 This creates a dbf file called outFile.dbf, not a csv file. Csv file output is not supported. Per the docs referenced: This tool can convert input tables to dBASE (.dbf), geodatabase (personal, file, or SDE), or INFO tables –  Regan Sarwas Jul 18 at 0:26
@ReganSarwas Thanks for the heads-up. I have edited the question to address your comments. The main idea here is that this is an alternative, simpler solution than converting to csv. –  Aaron Jul 18 at 2:58

ArcGIS already has a tool to do this called "Export Feature Attributes to ASCII" which is in the Spatial Statistics--> Utilities toolbox.

The advantage of this tool over "Table to Table" is that you can 1) define your delimiter (space, comma, tab), 2) choose the fields you want to export and 3) choose whether or not to export your field names to the CSV file. It also happens to be a Python script, so you could copy that file and make your own variant of it very easily.

So, if you want to create a model or script to export only the most recent features based on "last edit date field", simply precede the "Export Feature Attributes to ASCII" tool with a "Select Layer by Attribute" tool where you call out the query you want to run.

-