23

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.

31

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()
  • 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: – user29069 Apr 10 '14 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 '14 at 12:57
8

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")
  • +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 '14 at 19:46
1

Here is a piece of code I use. It helps me generates all my output files to .txt file with a range from 0,100. Hopefully it helps

for x in xrange(0,100):
    if os.path.isfile(outfolder + "/" + "outputs" + str(x) +".shp" ):
       inFeatures = "selected_features" + str(x) +".shp"
       export_ASCII = "ASCII " + str(x) +".txt"
       arcpy.ExportXYv_stats(inFeatures, ["Cur1_pr2","Cur3_pl1","slp1"],"SPACE", export_ASCII,"ADD_FIELD_NAMES")

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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