Skip to main content

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))