ArcPy调用SelectLayerByAttribute时SQL变量传参报错求助
解决ArcPy SelectLayerByAttribute参数错误问题
错误根源
你碰到的positional argument follows keyword string错误,本质是函数参数传递顺序违规:前两个参数用了关键字参数(in_layer_or_view = deer、selection_type = "NEW_SELECTION"),第三个参数却没指定参数名直接用位置参数,Python不允许位置参数出现在关键字参数之后。
正确实现方式
方式1:直接指定where_clause参数名
给第三个查询条件参数加上参数名where_clause=,同时保证SQL语句格式正确:
point_id = 375 arcpy.management.SelectLayerByAttribute( in_layer_or_view=deer, selection_type="NEW_SELECTION", where_clause=f'"Id" = {point_id}' )
方式2:用变量存储查询条件(可读性更强)
把SQL语句单独存为变量后再传入函数:
point_id = 375 where_clause = f'"Id" = {point_id}' arcpy.management.SelectLayerByAttribute( in_layer_or_view=deer, selection_type="NEW_SELECTION", where_clause=where_clause )
补充说明
- 如果
Id字段是文本类型而非数值类型,需要给point_id的值加单引号,比如:f'"Id" = \'{point_id}\'' - ArcGIS不同数据源的字段名引号规则有差异,也可以用方括号替代双引号,比如
f'[Id] = {point_id}',兼容性更好
内容的提问来源于stack exchange,提问作者Kori Kurtzeborn
相关产品推荐
相关产品推荐

