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


Instructions provided describe how to select a feature by 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.

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


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

  • Set up the geoprocessing environment and define the needed variables.

    # Import Modules...
    import arcgisscripting, os, sys, string

    # Create the Geoprocessor and set overwrites...
    gp = arcgisscripting.create()
    gp.OverwriteOutput = 1

    # Set Variables...
    gp.Workspace = r"D:\627514" # this is the folder that contains the data
    input = "testdata.shp" # the zip code shapefile
    lyr = "test_layer" # the name of the Make Feature Layer output

  • Convert the input shapefile into a Feature Layer, so it can be used in the Select Layer by Attributes and Location tools later.

    # Process: Make Feature Layer...
    gp.MakeFeatureLayer_management(input, lyr)

  • Create the text file, in which the output is written.

    # Create the Text File to write the zip values to...
    outFile = open(r"D:\627514\zipcode.txt", "w")

    If the text file does not already exist Python creates it.

  • Build the first Search Cursor to iterate through the polygon shapefile that contains the values in the field.

    # Build the Search Cursor...
    fcSearch = gp.SearchCursor(lyr, "", "", "ZIP")
    fc = fcSearch.Next()

  • Construct a loop that will run all the needed processes on each value from the field. In this case, Select Layer by Attribute and Select Layer by Location. The attribute selection needs a value for the field to be extracted out for each iteration, so an expression variable (exp) is needed to supply that variable.

    while fc:
    exp = "ZIP" + "='" + str(fc.getvalue("ZIP"))+"'"
    # Process: Select by Attribute...
    gp.SelectLayerbyAttribute_management(lyr, "NEW_SELECTION", exp)

    # Process: Select by Location...
    gp.SelectLayerbyLocation_management(lyr, "BOUNDARY_TOUCHES", lyr, "", "NEW_SELECTION")

  • Build another Search Cursor to iterate through the values from the above selection, and write the value of the Select Layer by Attribute to the text file. Then iterate through the entire selection, writing all the values to the text file. This step is completely contained within the first loop and ends with the method to start the initial loop over.

    # Build another Search Cursor to grab the zip values out of lyr...
    txtSearch = gp.SearchCursor(lyr, "", "", "ZIP")
    txtS = txtSearch.Next()
    outFile.write("Zip Code: " + str(fc.getvalue("ZIP"))+ "\n")
    while txtS:
    zval = str(txtS.getvalue("ZIP"))
    outFile.write(zval + "\n")
    txtS = txtSearch.Next()

    fc = fcSearch.Next()

  • Outside the loop, close the text file and release all variables from memory.

    outFile.close() # this closes the text file
    del gp, input, lyr, fcSearch, fc, exp, outFile, zval, txtS, txtSearch

  • The text file is formatted as follows:

    Zip Code: 08003
    Zip Code: 08004

Related Information