2014-12-04 82 views
0

我試圖通過Shapefile的字段的唯一值進行循環。該領域被命名爲AGLOMERADOS,我想循環throgh他們。 一旦我有這個列表,我想開始選擇屬性我的形狀文件,併爲每個選擇創建一個shapefile。 我得到空shapefile! :( 使用看起來代碼IM是這樣的:通過循環throgh唯一屬性值創建新圖層

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 = '布蘭卡', 'Cipolletti型', 'CONCORDIA', 'FORMOSA' ,...

然而,我的目標是= [u'BAHIA BLANCA',u'CIPOLLETTI',u'CONCORDIA',u'FORMOSA',u'GRAN CORDOBA'....]爲什麼? !?!??

謝謝!

回答

1

的 'U' 表示該字符串的格式爲Unicode。當p打印一個列表,它將包含該標籤來指示字符串格式。如果你運行下面的代碼:

​​

輸出將

BAHIA BLANCA 
CIPOLLETTI 
CONCORDIA 
FORMOSA 
GRAN CORDOBA 
0

的問題與您的SQL語句。您正在嘗試輸入lugares作爲變量,但它是您的字符串的一部分。

取而代之的是:

arcpy.SelectLayerByAttribute_management("pais","NEW_SELECTION",""""AGLOMERADO" = 'lugares'""") 

試試這個:

arcpy.SelectLayerByAttribute_management("pais","NEW_SELECTION", "\"AGLOMERADO\" = '" + lugares + "'") 

我希望這有助於!

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

+1

請添加一些說明,以及。 – BlackBeard 2018-01-10 10:03:18