在一个字段中找到重复项,然后用Python(ArcGIS)用Y或N更新另一个字段

2024-10-03 17:15:52 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试创建一个python脚本,它将用Y或N(可能超过5000条记录)来标识点形状文件中的重复记录。与此类似:

xyCombine | d应用程序

电话:836814.148873 N814378.125749

电话:836814.148873 N814378.125749

电话:836815.033548 N814377.614688 |

电话:836815.033548 N814377.614688 |

电话:836815.033548 N814377.614688 |

电话:E836818.016542 N814371.411850|

我希望处理字段xyCombine以查找重复项,如果另一个字段(dplicate)是否重复,则用Y或N更新它。预期结果为:

xyCombine | d应用程序

电话:836814.148873 N814378.125749

电话:836814.148873 N814378.125749

电话:E836815.033548 N814377.614688 | Y

电话:E836815.033548 N814377.614688 | Y

电话:E836815.033548 N814377.614688 | Y

E836818.016542北纬814371.411850

以下是我的尝试:

# Process: Searches xyCombine field for any duplicates
duplicateCount = 0
inShapefile = pointsShapefile
fieldName = "xyCombine"
shpFRows = arcpy.UpdateCursor(inShapefile)
shpFRow = shpFRows.next()
fieldList = []
while shpFRow:
    if shpFRow.isNull(fieldName) == False and len(str(shpFRow.getValue(fieldName)).strip()) > 1:
            fieldList.append(shpFRow.getValue(fieldName))
    shpFRow = shpFRows.next()
duplicateList = [x for x, y in collections.Counter(fieldList).items() if y > 1]
print duplicateList
selectFile = pointsShapefile
selectFields = ('xyCombine','dupCHK')
shpFRows = arcpy.UpdateCursor(selectFile,selectFields)
shpFRow1 = shpFRows.next()
while shpFRow1:
    if shpFRow1.isNull(fieldName) == False and len(str(shpFRow1.getValue(fieldName)).strip()) > 1:
        for row in duplicateList:
            if shpFRow1.getValue(fieldName) == row:
                duplicate += 1
                row[1] = "Y"
            else:
                row[1] = "N"
            cursor.updateRow(row)
        shpFRow1 = shpFRows.next()
if duplicateCount > 0:
    print ""
    print "*** "+str(duplicate)+" duplicated points. ***"
    print ""

如果我不包括

^{pr2}$

脚本执行正确,打印出重复的总数,但是不会用Y或N值更新字段duplicates,这很重要,因为它将在脚本后面提供csv错误报告。在

但是,当我包含它时,我会收到以下错误消息:

Python2.7.2(默认值,2011年6月12日,15:08:59)[MSCV.1500 32位(Intel)]在win32上

[u'E836814.148873 N814378.125749',u'E836815.033548 N814377.614688',u'E836818.016542 N814371.41185']

回溯(最近一次呼叫): 文件“C:\Duplicate Points Check\Python Scripts\DuplicatePointsCheck_TEST1.py”,第458行 重复点检查() 文件“C:\Duplicate Points Check\Python Scripts\DuplicatePointsCheck_TEST1.py”,第94行,DuplicatePointsCheck 行[1]=“N” TypeError:“unicode”对象不支持项分配>;>

我知道ArcGIS中有一些工具可以通过field calculator提供可能的解决方案。不过,我想加强对Python的理解,因为我对Python还很陌生。如果这个问题以前有人提出过,我很抱歉,但是我在互联网上搜索过,唯一的搜索结果包括找到和删除重复的记录。如果你们中有人能把我引向正确的方向,那将是很大的帮助。提前谢谢你。在


Tags: 文件脚本forifnextrowfieldname电话
1条回答
网友
1楼 · 发布于 2024-10-03 17:15:52

没有足够的信息可以确定,但似乎您使用的是ArcGIS 10.1或更高版本。如果是这样的话,看起来您正在尝试使用UpdateCursor的新数据访问版本,但实际上调用的是UpdateCursor的pre<;10.0版本。在

我最近没有使用过ArCGIS 10.0,但是从文档来看,语法似乎已经改变了。{使用^ecursa1为arcursa1赋值的字段:

for row in cursor:
    # field2 will be equal to field1 multiplied by 3.0
    row.setValue(field2, row.getValue(field1) * 3.0)
    cursor.updateRow(row)

您似乎在使用数据访问语法,如下所示,from the ArcGIS 10.2 documentation

^{pr2}$

确保你正在使用arcpy.da.UpdateCursor创建你的光标;我希望这能解决你的问题。在

相关问题 更多 >