ArcPy脚本执行报错:第13行存在无效字符问题排查求助
ArcPy Select工具报错:无效字符问题
此前可正常运行的ArcPy脚本现在持续报错,错误指向第13行的arcpy.analysis.Select工具调用,提示存在无效字符。
报错关联代码行
arcpy.analysis.Select(in_features=TCG4232_USNG_GRIDS, out_feature_class=TCG4232_USNG_GRIDS_Selection, where_clause="")
完整脚本
import arcpy #Set variables arcpy.mp.ArcGISProject('CURRENT') aprx = arcpy.mp.ArcGISProject('CURRENT') TCG4232_USNG_GRIDS = "TCG4232_USNG_GRIDS" Output_Folder = arcpy.GetParameterAsText(0) #Execute Select arcpy.env.overwriteOutput = True TCG4232_USNG_GRIDS_Selection = "P:/PROJECTS/RP_Quality_Control_cGIS/MapSalesArcProProject/MapSalesLive1211/MapSalesLive/ExportedGrids.gdb/TCG4232_USNG_GRIDS_Selection" arcpy.analysis.Select(in_features=TCG4232_USNG_GRIDS, out_feature_class=TCG4232_USNG_GRIDS_Selection, where_clause="") #Add selected grid to map contents arcpy.env.workspace = r"P:/PROJECTS/RP_Quality_Control_cGIS/MapSalesArcProProject/MapSalesLive1211/MapSalesLive/ExportedGrids.gdb" aprx = arcpy.mp.ArcGISProject('CURRENT') map = aprx.listMaps("Map")[0] map.addDataFromPath(TCG4232_USNG_GRIDS_Selection) #list of layer names that you want to be turned off. p = arcpy.mp.ArcGISProject("Current") m = p.listMaps("Map")[0] layer_names = ['TCG4232_USNG_GRIDS_Selection','TCG4232_USNG_GRIDS'] lyrList = m.listLayers() for lyr in lyrList: lyr.visible = True if lyr.name in layer_names: lyr.visible = False #Print to PDF try: aprx = arcpy.mp.ArcGISProject('CURRENT') l = aprx.listLayouts()[0] l.mapSeries.refresh() if l.mapSeries is not None: ms = l.mapSeries if ms.enabled: ms = l.mapSeries indexLyr = "TCG4232_USNG_GRIDS_Selection" ms.exportToPDF(Output_Folder,"ALL", "", "PDF_SINGLE_FILE", 150, "FASTEST", True, "ADAPTIVE", True, "LAYERS_ONLY", True, 80, True, False) except Exception as e: print(f"Error: {e.args[0]}") #Delete selected layer arcpy.management.Delete("TCG4232_USNG_GRIDS_Selection", '')
排查与解决建议
- 修正
where_clause参数:空字符串作为where子句可能触发语法校验问题,替换为where_clause="1=1"(等效选择所有要素,语法更合规) - 校验输出路径:确认输出GDB路径存在且有读写权限,路径中无特殊字符(如非ASCII符号、全角字符)
- 清理残留数据:手动删除GDB中已存在的
TCG4232_USNG_GRIDS_Selection要素类,避免覆盖冲突 - 优化变量定义:脚本中多次重复创建
aprx对象,可能引发上下文冲突,改为仅在开头定义一次并复用:# 开头只定义一次aprx aprx = arcpy.mp.ArcGISProject('CURRENT') - 检查输入图层状态:确认地图中的
TCG4232_USNG_GRIDS图层未损坏,数据源路径未变更
修改后的示例代码片段
import arcpy #Set variables aprx = arcpy.mp.ArcGISProject('CURRENT') TCG4232_USNG_GRIDS = "TCG4232_USNG_GRIDS" Output_Folder = arcpy.GetParameterAsText(0) #Execute Select arcpy.env.overwriteOutput = True TCG4232_USNG_GRIDS_Selection = "P:/PROJECTS/RP_Quality_Control_cGIS/MapSalesArcProProject/MapSalesLive1211/MapSalesLive/ExportedGrids.gdb/TCG4232_USNG_GRIDS_Selection" # 替换where_clause为合规写法 arcpy.analysis.Select(in_features=TCG4232_USNG_GRIDS, out_feature_class=TCG4232_USNG_GRIDS_Selection, where_clause="1=1") #Add selected grid to map contents arcpy.env.workspace = r"P:/PROJECTS/RP_Quality_Control_cGIS/MapSalesArcProProject/MapSalesLive1211/MapSalesLive/ExportedGrids.gdb" map = aprx.listMaps("Map")[0] map.addDataFromPath(TCG4232_USNG_GRIDS_Selection) #list of layer names that you want to be turned off. m = aprx.listMaps("Map")[0] layer_names = ['TCG4232_USNG_GRIDS_Selection','TCG4232_USNG_GRIDS'] lyrList = m.listLayers() for lyr in lyrList: lyr.visible = True if lyr.name in layer_names: lyr.visible = False #Print to PDF try: l = aprx.listLayouts()[0] l.mapSeries.refresh() if l.mapSeries is not None: ms = l.mapSeries if ms.enabled: indexLyr = "TCG4232_USNG_GRIDS_Selection" ms.exportToPDF(Output_Folder,"ALL", "", "PDF_SINGLE_FILE", 150, "FASTEST", True, "ADAPTIVE", True, "LAYERS_ONLY", True, 80, True, False) except Exception as e: print(f"Error: {e.args[0]}") #Delete selected layer arcpy.management.Delete("TCG4232_USNG_GRIDS_Selection", '')
内容的提问来源于stack exchange,提问作者Allen Aguas
相关产品推荐
相关产品推荐

