English

How To: Extract values from a field and write them to a text file using Python at ArcGIS 10.x

Summary

Instructions provided describe how to select a feature by an attribute, select all the features that share a boundary with it, and then export the values of all of the features to a text file. This article is specific to using the ArcPy module installed with ArcGIS 10.x.

To complete this procedure using Python requires the use of the Search Cursor method to iterate through the values of the field.

Procedure

Below are the general steps for the procedure followed by code examples using a zip code polygon shapefile.

  1. Set up the environment and define needed variables:
    Code:
    # Import modules
    import arcpy, os, sys, string

    # Create envrionmental variables
    arcpy.env.overwriteOutput = True
    arcpy.env.workspace = r"D:\Data" # This is the folder that contains the data

    # Set variables
    input = "testdata.shp" # The zip code shapefile
    lyr = "test_layer" # The name of the Make Feature Layer output

  2. Convert the input shapefile into a Feature Layer, so it can be used in the Select Layer by Attributes and Location tools.
    Code:
    # Processes to make feature layer...
    arcpy.MakeFeatureLayer_management(input, lyr)

  3. Create the text file to which the output is to be written. If it does not already exist, Python will create it in this step.
    Code:
    # Create the text file to write the zip values to...
    outFile = open(r"D:\Data\zipcodes.text", "w")

  4. Build the first Search Cursor to iterate through the polygon shapefile that contains the values in the field. There is a second kind of cursor that can be used at version 10.1 and later; skip to the final step for that sample.
    Code:
    # Build search cursor for all 10.x...

    fcSearch = arcpy.SearchCursor(lyr, "", "", "ZIP")

  5. Construct a loop that runs all the needed processes on each value from the field, in this case, Select Layer by Attribute and Select Layer by Location. The Select by Attribute needs a specific value from the field for each iteration, so an expression variable (exp) is created for this.
    Code:
    for fcRow in fcSearch:
    # Create expression based on the current row's zip code value
    exp = '"ZIP" = ' + "'" + str(fcRow.getValue("ZIP")) + "'"
    # Process: Select Layer by Attribute...
    arcpy.SelectLayerByAttribute_management(lyr, "NEW_SELECTION", exp)
    # Process: Select Layer by Location...
    arcpy.SelectLayerByLocation_management(lyr, "BOUNDARY_TOUCHES", lyr, "", "NEW_SELECTION")

  6. Build another search cursor within this loop to iterate through all the values associated with the rows found in the Select by Location process and write those values in the text file. This step is completely contained within the loop.
    Code:
    # Build another search cursor to grab the zip values out of lyr...
    txtSearch = arcpy.SearchCursor(lyr, "", "", "ZIP")
    outFile.write("Zip Code: " + str(fcRow.getValue("ZIP")) + "\n")
    for txtRow in txtSearch:
    zval = str(txtRow.getValue("ZIP"))
    outFile.write(zval + "\n")

  7. Outside of the loop, close the text file and release all the variables from memory.
    Code:
    outFile.close() # This closes the text file
    del input, lyr, fcRow, txtRow, txtSearch, fcSearch, outFile, zva

  8. If using the Data Access Search Cursor in this process, the final part of the code is similar to this:
    Code:
    # Build data access search cursor for 10.1+
    fcSearch = arcpy.da.SearchCursor(lyr, ["ZIP"])
    for fcRow in fcSearch:
    exp = '"ZIP" = ' + "'" + str(fcRow[0]) + "'"
    # Process: Select Layer by Attribute...
    arcpy.SelectLayerByAttribute_management(lyr, "NEW_SELECTION", exp)
    # Process: Select Layer by Location...
    arcpy.SelectLayerByLocation_management(lyr, "BOUNDARY_TOUCHES", lyr, "", "NEW_SELECTION")

    # Build another search cursor to grab the zip values out of lyr...
    txtSearch = arcpy.da.SearchCursor(lyr, ["ZIP"])
    outFile.write("Zip Code: " + str(fcRow[0]) + "\n")
    for txtRow in txtSearch:
    zval = str(txtRow[0])
    outFile.write(zval + "\n")

    outFile.close() # This closes the text file
    del input, lyr, fcRow, txtRow, txtSearch, fcSearch, outFile, zval

Related Information