我有一个如下代码,我希望从torn_all层中选择日期等于year_month,例如2009-12将被选中之后。所以我应该加上2009-12%。但是年月都在变化。
import arcpy
from arcpy import env
years=['2010','2011','2012','2013']
months=["01","02","03","04","05","06","07","08","09","10","11","12"]
env.workspace="c:/twu/wind/torn/tonardo.gdb"
arcpy.MakeFeatureLayer_management("torn_all", "lyr")
for year in years:
for month in months:
arcpy.SelectLayerByAttribute_management("lyr","NEW_SELECTION",' "date" LIKE "%s-%s%"') %(year,month)
arcpy.CopyFeatures_management("lyr", "c:/twu/wind/torn/tonardo.gdb/torn_%s/torn_%s_%s") %(year,year,month)
arcpy.SelectLayerByAttribute_management("lyr","CLEAR_SELECTION")
错误是 Traceback(最近一次调用最后一次): 文件“C:/twu/wind/code/sep_month_tor.py”,第16行,in arcpy.SelectLayerByAttribute_management(“lyr”,“NEW_SELECTION”,“”date“LIKE”%s-%s%“')%(年,月) 在SelectLayerByAttribute中输入文件“C:\ Program Files(x86)\ ArcGIS \ Desktop10.2 \ arcpy \ arcpy \ management.py”,第6688行 提高e ExecuteError:ERROR 000358:表达式无效 无法执行(SelectLayerByAttribute)。
答案 0 :(得分:0)
string formatting的方法变得混乱:您尝试同时包含百分号%
并调用插值运算符%s
。它认为%
正在寻找第三个论点。
'"date" LIKE "{0}-{1}%"'.format(year,month)
'c:/twu/wind/torn/tonardo.gdb/torn_{0}/torn_{0}_{1}'.format(year,month)