# Export table to X,Y,Z ASCII file via arcpy

I'm looking for a way to export an ArcGIS table (created with the Sample command) 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. Thanks, Toni

-

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

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

#--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)))


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


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()

-
Excellent, thanks @Jason –  Toni Dec 14 '11 at 14:48
@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"