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'm looking for a way to export an ArcGIS table (created with the Sample tool) to a text file via ArcPy.

I can do this in ArcGIS via the context menu by right-clicking the table, but have not found a way to script this.

share|improve this question

2 Answers 2

up vote 21 down vote accepted

You can do this using a cursor to grab the data from your table and write to a comma-delimited text file.

EDIT: I'm adding a more concise block of code to accomplish the task using the csv module of Python

New Answer using arcpy.da cursor:

import arcpy,csv

table =r'c:\path\to\table'
outfile = r'c:\path\to\output\ascii\text\file'

#--first lets make a list of all of the fields in the table
fields = arcpy.ListFields(table)
field_names = [field.name for field in fields]

with open(outfile,'wb') as f:
    dw = csv.DictWriter(f,field_names)
    #--write all field names to the output file
    dw.writeheader()

    #--now we make the search cursor that will iterate through the rows of the table
    with arcpy.da.SearchCursor(table,field_names) as cursor:
        for row in cursor:
            dw.writerow(dict(zip(field_names,row)))

New Answer using old-style cursor:

import arcpy,csv

table =r'c:\path\to\table'
outfile = r'c:\path\to\output\ascii\text\file'      

#--first lets make a list of all of the fields in the table
fields = arcpy.ListFields(table)
field_names = [field.name for field in fields]

with open(outfile,'wb') as f:
    w = csv.writer(f)
    #--write all field names to the output file
    w.writerow(field_names)

    #--now we make the search cursor that will iterate through the rows of the table
    for row in arcpy.SearchCursor(table):
        field_vals = [row.getValue(field.name) for field in fields]
        w.writerow(field_vals)
    del row

Old answer:

import arcpy

table =r'c:\path\to\table'
outfile = r'c:\path\to\output\ascii\text\file'


#--first lets make a list of all of the fields in the table
fields = arcpy.ListFields(table)

i = 1
f = open(outfile,'w')
for field in fields:
    #--write all field names to the output file
    if i < len(fields):
        f.write('%s,' % field.name)
        i += 1
    else:
        f.write('%s\n' % field.name)

#--now we make the search cursor that will iterate through the rows of the table
rows = arcpy.SearchCursor(table)
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()
share|improve this answer
1  
Excellent, thanks @Jason –  Toni Dec 14 '11 at 14:48
    
Glad I could help you @Toni –  Jason Dec 14 '11 at 19:50
1  
@Jason - Thanks, this was very helpful. I'm new so I don't have the reputation to comment on your accepted answer. I think there's a small mistake in the new answer that uses an arcpy.da cursor. with arcpy.da.SearchCursor(table) as cursor: should be with arcpy.da.SearchCursor(table, field_names) as cursor: –  Tyler G Apr 10 at 18:58
    
Good catch @TylerG, I've edited the answer to include the list of fields required by the Data Access cursor. Thanks. –  Jason Apr 11 at 12:57

You may want the "Export Feature Attribute to ASCII", cleverly named arcpy.ExportXYv_stats

http://help.arcgis.com/en/arcgisdesktop/10.0/help/index.html#//005p0000003v000000

import arcpy

feature = "path to feature here"
# fieldnames must be explicitly provided. Note that you will get additional fields based on the feature type (e.g., "XCoord" and "YCoord" for point features)
fieldnames = [X.name for X in arcpy.ListFields(feature)]
# delimiter options "SPACE", "COMMA", or "SEMI-COLON"
# header options "ADD_FIELD_NAMES" or "NO_FIELD_NAMES"
arcpy.ExportXYv_stats(feature, fieldnames, "SPACE", "path to outfile", "ADD_FIELD_NAMES")
share|improve this answer
    
+1 for sleuthing! This works interactively but not as well in a model or script, because the field names must be specified. –  matt wilkie May 27 at 19:46

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.