ValidateFieldName

摘要

使用字符串(字段名称)和工作空间路径,并根据输出地理数据库中的名称限制返回有效的字段名称。 输入字符串中的所有无效字符都将替换为下划线 (_)。 字段名称限制取决于所使用的特定数据库(SQL 或 Oracle)。

语法

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

The field name to be validated. If the optional workspace is not specified, the field name is validated against the current workspace.

String
workspace

An optional specified workspace to validate the field name against. The workspace can be a file system or a file, or enterprise geodatabase.

If the workspace is not specified, the field name is validated using the current workspace environment. If the workspace environment has not been set, the field name is validated based on a folder workspace.

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!",
                                    "PYTHON_9.3")
except ShapeError as err:
    print(err[0])
except arcpy.ExecuteError:
    print(arcpy.GetMessages(2))

相关主题