ArcPy Cursor setValues

时间:2014-03-07 20:07:29

标签: python cursor arcpy

我正在尝试遍历字段,检查字段是否为空或空白并填写999999.脚本运行正常,但似乎没有执行if表达式,因为它不打印任何内容并且不不要改变任何字段的值。

fc = "C:\Users\\bbrock\Documents\ArcGIS\Ports.shp"

# Create a search cursor 
#
rows = arcpy.SearchCursor(fc) 

# Create a list of string fields
fields = arcpy.ListFields(fc, "", "String")

for row in rows:
    for field in fields:
        if field.type != "Geometry":
            if row.getValue(field.name) == '':
                row.setValue(field.name, 'ondciqwn')                
                print "%s: Value = %s" % (field.name, row.getValue(field.name))

            if row.isNull(field.name):
                row.setValue(field.name, 'bvadvfe')             
                print "%s: Value = %s" % (field.name, row.getValue(field.name))     

2 个答案:

答案 0 :(得分:0)

SearchCursor函数建立一个只读游标。相反,您可以使用UpdateCursor更新或删除行。您的代码将类似于:

import arcpy
rows = arcpy.UpdateCursor(fc) 
fields = arcpy.ListFields(fc, "", "String")
for row in rows:
    for field in fields:
        if field.type != "Geometry":
            if row.getValue(field.name) == '':
                row.setValue(field.name, 'ondciqwn')                
                print "%s: Value = %s" % (field.name, row.getValue(field.name))

            if row.isNull(field.name):
                row.setValue(field.name, 'bvadvfe')             
                print "%s: Value = %s" % (field.name, row.getValue(field.name)) 
    rows.updateRow(row)

答案 1 :(得分:0)

添加此项是因为建议使用with arcpy.da.updateCursor as...来更好地处理ArcGIS 10.1的异常处理和速度,并且由于它具有不同的属性,签名等,因此我能够通过重要的重写来实现这一功能。如下所示:

fc = "C:/Users/bbrock/Documents/ArcGIS/Default.gdb/Ports_Admin_Join"

fields = arcpy.ListFields(fc, "", "String")

for field in fields:
    with arcpy.da.UpdateCursor(fc, field.name) as rows:
        for row in rows:
            if field.type != "geometry" and field.length > 5:
                if row[0] == None or row[0] == ' ':
                    row[0] = '999999'
                    rows.updateRow(row)    
                    print "%s: Value = %s" % (field.name, row[0])