You can use this function:
import arcpy
import csv #if you have unicode characters in your table, use: import unicodecsv as csv
def export_table_as_txt(infile,outfile):
'''
Exports a feature classes table to a txt file.
'''
#--first lets make a list of all of the fields in the table
fields = arcpy.ListFields(infile)
field_names = [field.name for field in fields]
with open(outfile,'wb') as f:
dw = csv.DictWriter(f,field_names,delimiter=';')
#--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(infile,field_names) as cursor:
for row in cursor:
dw.writerow(dict(zip(field_names,row)))
In the line after the first with
statement, you can choose the delimiter. Use \t
instead of ;
if you want tab delimited output.
Then, call the function with something like this:
layer = "C:\my_folder\my_layer.shp" #can also be a path to a featureclass in a Geo-Database
export_outfile = "C:\my_folder\my_output.txt"
export_table_as_txt(layer,export_outfile)