【发布时间】:2015-03-07 14:13:22
【问题描述】:
我正在尝试创建一个 python 脚本,该脚本将识别一个带有 Y 或 N 的点 shapefile 中的重复记录(可能超过 5000 条记录)。类似这样:
xyCombine |复制
E836814.148873 N814378.125749 |
E836814.148873 N814378.125749 |
E836815.033548 N814377.614688 |
E836815.033548 N814377.614688 |
E836815.033548 N814377.614688 |
E836818.016542 N814371.411850 |
我希望处理字段 xyCombine 以查找重复项,并使用 Y 或 N 更新另一个字段(dplicate)(如果它是重复项)。期望的结果为:
xyCombine |复制
E836814.148873 N814378.125749 |是的
E836814.148873 N814378.125749 |是的
E836815.033548 N814377.614688 |是的
E836815.033548 N814377.614688 |是的
E836815.033548 N814377.614688 |是的
E836818.016542 N814371.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 ""
如果我不包括
row[1] = "Y"
else:
row[1] = "N"
cursor.updateRow(row)
脚本正确执行打印重复的总数,但不会使用 Y 或 N 值更新字段重复,这很重要,因为它将在脚本后面提供 csv 错误报告。
但是,当我包含它时,我收到以下错误消息:
Win32 上的 Python 2.7.2(默认,2011 年 6 月 12 日,15:08:59)[MSC v.1500 32 位(英特尔)]
[u'E836814.148873 N814378.125749', u'E836815.033548 N814377.614688', u'E836818.016542 N814371.41185']
Traceback(最近一次调用最后一次): 文件“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 中有一些工具可以通过字段计算器提供可能的解决方案。但是我想加强我对 Python 的理解,因为我对 Python 还是很陌生。如果之前有人提出过这个问题,我深表歉意,但我已经在互联网上进行了搜索,我的唯一搜索结果包括定位和删除重复记录。如果你们中的任何一个人可以引导我朝着正确的方向前进,那将是非常有帮助的。提前谢谢你。
【问题讨论】:
-
示例文件中的记录已排序。您的真实文件中的记录是否也已排序?
-
@gboffi:不,真实文件中的记录没有排序。我只是想澄清我的问题。
标签: python python-2.7 arcgis