Skip to content
TheChatArchiveThe Chat Archive
gpt-3.5-turbo-0613· OpenAIAi2 WildChat-4.8M (AI2)

import arcpy import os import re inputpolygon =…

  1. User

    import arcpy import os import re

    input_polygon = r"F:\wushenqi\tif\data\wushenqi_3\vci\d_8735"

    arcpy.env.workspace = input_polygon shps = arcpy.ListFiles("*.shp")

    for shp in shps: catchment_name = shp[8:-4]

    print (catchment_name)
    
    date = int(catchment_name)
    
    field_names = [f.name for f in arcpy.ListFields(shp)]
    if 'landId' not in field_names:
        if 'date' not in field_names:
            arcpy.AddField_management(shp, 'date', 'Long')
            arcpy.AddField_management(shp, 'landId', 'short')
    
    arcpy.CalculateField_management(shp,'date',expression=date,expression_type="VB", code_block="")
    arcpy.CalculateField_management(shp,'landId',expression=8735)
    

    Traceback (most recent call last): File "f:/wushenqi/py/dbf.py", line 25, in arcpy.CalculateField_management(shp,'landId',expression=8735) File "C:\Program Files (x86)\ArcGIS\Desktop10.5\ArcPy\arcpy\management.py", line 3663, in CalculateField raise e arcgisscripting.ExecuteError: Failed to execute. Parameters are not valid. ERROR 000728: Field landId does not exist within table Failed to execute (CalculateField) 怎么解决

  2. Assistant

    这个错误是因为您尝试在shp文件中的字段"landId"上进行计算,但该字段不存在。您可以使用arcpy.ListFields函数检查shp中的字段列表,以确认"landId"字段是否存在。如果不存在,您可以使用arcpy.AddField_management函数添加该字段,然后再次尝试计算它的值。

    修改代码如下:

    import arcpy
    import os
    import re 
    
    input_polygon = r"F:\wushenqi\tif\data\wushenqi_3\vci\d_8735"
    
    arcpy.env.workspace = input_polygon
    shps = arcpy.ListFiles("*.shp")
    
    for shp in shps:
        catchment_name = shp[8:-4]
       
        print (catchment_name)
        
        date = int(catchment_name)
    
        field_names = [f.name for f in arcpy.ListFields(shp)]
        if 'landId' not in field_names:
            if 'date' not in field_names:
                arcpy.AddField_management(shp, 'date', 'Long')
                arcpy.AddField_management(shp, 'landId', 'Short')  # 注意大小写,short改为Short
    
        arcpy.CalculateField_management(shp, 'date', expression=date, expression_type="PYTHON")
        arcpy.CalculateField_management(shp, 'landId', expression='8735', expression_type="PYTHON")
    

    请注意,我还将计算字段时的表达式类型从"VB"更改为"PYTHON"。