我试图循环遍历shapefile字段的唯一值。该字段名为AGLOMERADOS,我想循环throgh他们。 一旦我有了这个列表,我想开始按属性选择我的shape文件,并为每个选择创建一个shapefile。 我得到空的shapefile !!! :( 使用的代码看起来像这样:
import os, arcpy, numpy
from arcpy import env
arcpy.env.overwriteOutput = True
def unique_values(table, field):
with arcpy.da.SearchCursor(table, [field]) as cursor:
return sorted({row[0] for row in cursor})
agloms=unique_values(r'C:\Users\gdorna\Dropbox\CIPUV\lilp\Proyecto LILP 2014 - Infraestructura\Lincoln Infra - GIS\Iterate agloms\agloms.gdb\pais','AGLOMERADO')
i=0
for lugares in agloms:
arcpy.SelectLayerByAttribute_management("pais","NEW_SELECTION",""""AGLOMERADO" = 'lugares'""")
arcpy.CopyFeatures_management('pais', "a_" + `i`)
print `lugares` + "----->" + `i`
i=i+1
我应该看到的列表应该是这样的, agloms ='BAHIA BLANCA','CIPOLLETTI','CONCORDIA','FORMOSA',......
然而,我的agloms = [u'BAHIA BLANCA',u'CIPOLLETTI',u'CONCORDIA',u'FORMOSA',u'GRAN CORDOBA'......]为什么会这样?!?!??
谢谢!
答案 0 :(得分:1)
'u'表示字符串的格式是unicode。当您打印列表时,它将包含该标记以指示字符串格式。如果您要运行以下代码:
for lugar in agloms:
print lugar
输出将是
BAHIA BLANCA
CIPOLLETTI
CONCORDIA
FORMOSA
GRAN CORDOBA
答案 1 :(得分:0)
问题在于您的SQL语句。您正在尝试输入 lugares 作为变量,但它是您的字符串的一部分。
而不是:
arcpy.SelectLayerByAttribute_management("pais","NEW_SELECTION",""""AGLOMERADO" = 'lugares'""")
试试这个:
arcpy.SelectLayerByAttribute_management("pais","NEW_SELECTION", "\"AGLOMERADO\" = '" + lugares + "'")
我希望这有帮助!
答案 2 :(得分:0)
import os, arcpy
from arcpy import env
arcpy.env.overwriteOutput = True
def unique_values(table, field):
with arcpy.da.SearchCursor(table, [field]) as cursor:
return sorted({row[0] for row in cursor})
agloms = unique_values(table, field)
i=21
for lugares in agloms:
arcpy.MakeFeatureLayer_management(table, "a_" + 'i+1', "\"field\" = '" + lugares + "'")
arcpy.FeatureClassToFeatureClass_conversion("a_" + 'i+1', r'your out_path', "a_" + lugares)
print lugares + "----->" + 'i'
i=i+1