Feature Attributes to CSV

Ever need to export your feature attributes or a table to a CSV file when using ArcGIS Desktop?  There are a few ways to do it.  Here are four (plus an extra):

Option 1: Table to Excel to CSV

This procedure uses the Table to Excel tool, then Microsoft Excel to convert to CSV.

The Table to Excel tool is located in the Conversion Tools toolbox.

csv1

csv2

Once you save your Excel file, open it up in Microsoft Excel and save it to a CSV file.

csv3

csv4

Not very painful, but there are a few steps here.

Option 2: Export Table in ArcMap

This option exports your attribute table in ArcMap to a CSV file.

Open your features in ArcMap, then open the attribute table.

csv5

Click on the Table Options button and select Export.

csv6

The Export Data window will appear.

csv7

Click on the yellow file folder icon to open the file browser.  Once open, select the “Save as type” pulldown and select “Text File”.  Now rename your file in the Name field and make sure it ends in the .CSV extension.

csv8

Click on the Save button, then back at the Export Data window click the OK button.  Your CSV file is saved.  You will be prompted if you want to add the new table to your map, click the No button.

csv9

Note that using this option some numeric fields might have too many numbers after the decimal place in the CSV file.

Option 3: Export Feature Attributes to ASCII

This option uses the Export Feature Attribute to ASCII Tool.  The tool is located in the Spatial Statistics Tools toolbox.

csv10

csv11

Note that with this tool, you can select what fields you want to export, what delimiter to use, and if you want field name headers.  You can delimit the data by a space, comma, or semi-colon.  Once you fill it all out and name your file with the .CSV extension, click on the OK button and your CSV file is saved.

csv12

Note that you get the added bonus of coordiates saved to the CSV file.  In this case, the centroid point of the state polygons.  Also note that the X and Y coordinate values have eight significant digits of precision, while floating-point attribute values have six significant digits … which means your numeric field values might have too many numbers after the decimal place.

Option 4: Python Script

Ah yes, almighty Python!  Use this python script to convert feature attributes or a table to a CSV file.  Just change the input_fct, output_csv, and csv_delimiter variables.  Note I use the vertical bar character “|” for my delimiter.  You can change it to whatever you want.  I suggest using a character you do not have in any attributes.

# ---------------------------------------------------------------------------
# tocsv.py
# Created on: 11/29/2017
# By: Michael A. Carson
# Description: Exports feature attributes or a table to a CSV file.
# Note: Make changes to the input_fct, output_csv, and csv_delimiter
#       variables for your data.
# ---------------------------------------------------------------------------

# Change these variable values for your data

input_fct = "C:/temp/demo_data.gdb/states"
output_csv = "C:/temp/states4.csv"
csv_delimiter = "|"

# Import modules

import arcpy,csv

# Set overwrite output if the same name

arcpy.env.overwriteOutput = True

# Read in fields and get field names

fld_list = arcpy.ListFields(input_fct)
fld_names = [fld.name for fld in fld_list]

# Open the CSV file and write out field names and data

with open(output_csv, 'wb') as csv_file:
  writer = csv.writer(csv_file, delimiter=csv_delimiter)
  writer.writerow(fld_names)
  with arcpy.da.SearchCursor(input_fct, fld_names) as cursor:
    for row in cursor:
      writer.writerow(row)
  csv_file.close()

# All done.

print "Created " + output_csv

After running the script, here is my result:

csv13

Note field “Shape” came over to give the centroid coordinates for each of the state polygons.  Also note that the values from the numeric fields look good too.  Nice!

Possible Option 5: Convert Table to CSV File tool

There is one more option, but you must have the Roads and Highways extension.  I do not, so I cannot demonstratre it here, however, if you do the tool is located in the Roads and Highways toolbox.  You can read about the tool here.  It looks like the tool allows you to use either a comma or a pipe (vertical bar) as a delimiter.

I hope this information helps you out with your CSV file needs!  -mike


			

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s