ValidateFieldName

摘要

返回基于字段名称和数据库路径的数据库有效字段名称。 输入字段名称中的所有无效字符都将替换为下划线。 字段名称限制取决于数据库类型。

语法

ValidateFieldName (name, {workspace})
参数说明数据类型
name

The field name that will be validated.

String
workspace

The workspace that will be used to validate the field name. The workspace can be a file system, a file, or an enterprise geodatabase.

If the workspace is not specified, the Current Workspace environment will be used. If the Current Workspace environment is not set, a folder workspace will be used.

String
返回值
数据类型说明
String

工作空间的有效字段名称。

代码示例

ValidateFieldName 示例

将返回基于工作空间的有效字段名称。

import arcpy
import os

class ShapeError(Exception):
    pass


try:
    # Get the input feature class and make sure it contains polygons.
    in_fc = arcpy.GetParameterAsText(0)
    if arcpy.Describe(input).shapeType != "Polygon":
        # Raise a custom exception
        raise ShapeError("Input does not contain polygons")

    # Get the new field name and validate it.
    field_name = arcpy.GetParameterAsText(1)
    field_name = arcpy.ValidateFieldName(field_name, os.path.dirname(in_fc))

    # Add the new field and calculate the value.
    arcpy.management.AddField(in_fc, field_name, "DOUBLE")
    arcpy.management.CalculateField(
        in_fc, field_name, "!shape.area! / !shape.length!", "PYTHON3"
    )
except ShapeError as err:
    print(err)
except arcpy.ExecuteError:
    print(arcpy.GetMessages(2))

相关主题