简体   繁体   English

ArcGIS中的Python。 需要访问字符串元素

[英]Python in ArcGIS. Need get access to elements of strings

ArcGIS 10.0 ArcGIS 10.0

I used arcpy.UpdateCursor for access to my field: 我使用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)

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 ? 您能推荐其他方法吗?

The ArcGIS help pages about UpdateCursors and Accessing data using cursors are useful background information. 有关UpdateCursors使用光标访问数据的ArcGIS帮助页面是有用的背景信息。

The UpdateCursor function creates a "cursor" object, which is made up of "row" objects. UpdateCursor函数创建一个“光标”对象,该对象由“行”对象组成。 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. 要从row对象访问数据(无论是读取还是更新),您需要指定字段名称而不是数字索引。 So, if you're checking whether attribute letter is equal to E : 因此,如果您要检查属性letter是否等于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: 一旦将该属性值存储在另一个变量中,就可以访问结果字符串的元素,并进行所需的任何Python字符串操作:

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

Problem resolve. 解决问题。 I replace "field" to field and now works fine. 我将“ field”替换为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)

Work with the newer version of cursors: arcpy.da.UpdateCursor and you will have the row[field] indexing for which I believe you were originally looking. 使用较新版本的游标:arcpy.da.UpdateCursor,您将获得row [field]索引,我相信您最初正在寻找该索引。

You have to be using ArcGIS >= 10.1 for access to da. 您必须使用ArcGIS> = 10.1来访问da。 cursors. 游标。 ArcGIS 10.3 Help for arcpy.da ArcGIS 10.3帮助(针对arcpy.da)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM