Search code examples
pythoncursorarcgisarcpy

Python in ArcGIS. Need get access to elements of strings


ArcGIS 10.0

I used arcpy.UpdateCursor for access to my field:

import arcpy
import sys

layer = sys.argv[1]#my table
field = sys.argv[2]#target field in table

cursor = arcpy.UpdateCursor(layer)

for row in cursor:
    attrString = row.getValue("field")
    subString = attrString[3]
    row.setValue(field,subString)
    cursor.updateRow(row)

My problem is that I want to access the element of string, which has variable "row", but is it not support indexes and not iterable. Can you please recommend other methods ?


Solution

  • The ArcGIS help pages about UpdateCursors and Accessing data using cursors are useful background information.

    The UpdateCursor function creates a "cursor" object, which is made up of "row" objects. Each of these rows represents an individual feature of the feature class or layer.

    To access data (whether reading or updating) from a row object, you need to specify the field name instead of a numerical index. So, if you're checking whether attribute letter is equal to E:

    if row.getValue("letter") == "E":
        # do stuff
    

    Once you have gotten that attribute value stored in another variable, you can access elements of the resulting string and do any Python string manipulation you want:

    attrString = row.getValue("street")
    subString = attrString[11]
    newString = attrString.replace("B", "t")
    attrString[11] = "t"