Python 在 ArcGIS 中。需要访问字符串元素
Python in ArcGIS. Need get access to elements of strings
ArcGIS 10.0
我使用 arcpy.UpdateCursor 访问我的字段:
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)
我的问题是我想访问字符串的元素,它有变量"row",但它不支持索引并且不可迭代。
你能推荐其他方法吗?
有关 UpdateCursors and Accessing data using cursors 的 ArcGIS 帮助页面是有用的背景信息。
UpdateCursor
函数创建一个"cursor"对象,它由"row"个对象组成。这些行中的每一行代表特征 class 或图层的一个单独特征。
要从 row
对象访问数据(无论是读取还是更新),您需要指定 字段名称 而不是数字索引。因此,如果您要检查属性 letter
是否等于 E
:
if row.getValue("letter") == "E":
# do stuff
一旦您将该属性值存储在另一个变量中,您就可以访问结果字符串的元素并执行任何您想要的 Python 字符串操作:
attrString = row.getValue("street")
subString = attrString[11]
newString = attrString.replace("B", "t")
attrString[11] = "t"
问题解决。我将 "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)
使用较新版本的游标:
arcpy.da.UpdateCursor 您将拥有我相信您最初寻找的行[字段] 索引。
您必须使用 ArcGIS >= 10.1 才能访问 da.光标。
ArcGIS 10.3 Help for arcpy.da
ArcGIS 10.0
我使用 arcpy.UpdateCursor 访问我的字段:
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)
我的问题是我想访问字符串的元素,它有变量"row",但它不支持索引并且不可迭代。 你能推荐其他方法吗?
有关 UpdateCursors and Accessing data using cursors 的 ArcGIS 帮助页面是有用的背景信息。
UpdateCursor
函数创建一个"cursor"对象,它由"row"个对象组成。这些行中的每一行代表特征 class 或图层的一个单独特征。
要从 row
对象访问数据(无论是读取还是更新),您需要指定 字段名称 而不是数字索引。因此,如果您要检查属性 letter
是否等于 E
:
if row.getValue("letter") == "E":
# do stuff
一旦您将该属性值存储在另一个变量中,您就可以访问结果字符串的元素并执行任何您想要的 Python 字符串操作:
attrString = row.getValue("street")
subString = attrString[11]
newString = attrString.replace("B", "t")
attrString[11] = "t"
问题解决。我将 "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)
使用较新版本的游标: arcpy.da.UpdateCursor 您将拥有我相信您最初寻找的行[字段] 索引。
您必须使用 ArcGIS >= 10.1 才能访问 da.光标。 ArcGIS 10.3 Help for arcpy.da