Take the 2-minute tour ×
Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. It's 100% free, no registration required.

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.

share|improve this question
    
How do you post what you have so far? New to stackexchange also –  user35267 Jul 31 at 14:07
2  
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 at 14:16

2 Answers 2

I see in your script you are using arcpy, 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 .csv file.

import arcpy

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

arcpy.TableToTable_conversion (fc, outws, 'outFile.csv')
share|improve this answer

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.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.