Problem:  When using Calculate Field in Python to update a text field, a 'value is not defined' message causes the script to fail

相关信息
Article ID: 40577
Bug Id: N/A
Software:
ArcGIS - ArcEditor 10
ArcGIS - ArcInfo 10
ArcGIS - ArcView 10
ArcGIS for Desktop Advanced 10.1
ArcGIS for Desktop Standard 10.1
ArcGIS for Desktop Basic 10.1
Platforms:
Windows XP, Server 2003, Vista, Server 2008, Windows 7, Windows 8, Server 2012

问题描述
In a stand-alone Python IDE or ArcMap's Python window, when attempting to update a field with a string value using arcpy.CalculateField_management, the error message: name 'TEXT' is not defined; 'Failed to execute (Calculate Field)' may occur.

For example:


Runtime error <class 'arcgisscripting.ExecuteError'>:
ERROR 000539: Error running expression:TEXT <type 'exceptions.NameError'>:
name 'TEXT' is not defined Failed to execute (CalculateField).


原因
The cause for this error message is related to the way that Python reads string values. Typically, when entering a string in a Python script, double quotation marks are used around the string, as in: "TEXT".

When using the Python parser in ArcMap's Field Calculator, a string is defined by using single quotes: 'TEXT'.

Using arcpy.CalculateField_management in a script requires the use of the same formatting used by the Field Calculator Python parser (that is, single quotes) in addition to the Python script formatting (double quotes). Keep this in mind when making a variable to pass into this tool as well.
已邀请:

EsriSupport

赞同来自:

解决方案
The following is an example of the correct syntax for arcpy.CalculateField_management to update a field with a string value.


When using arcpy.CalculateField_management, expressions that contain strings must be formatted as follows: "'TEXT'" The structure is: double quote, single quote, text, single quote, double quote. This allows the Python parser to recognize the string as defined, as shown in the code example below.
arcpy.CalculateField_management("FeatureClass", "FieldName", "'Hello_World'", "PYTHON_9.3")




其它相关参考
  1. Calculate Field (Data Management)


创建及修改时间
Created: 9/28/2012

Last Modified: 7/12/2013
原文链接
http://support.esri.com/en/kno ... 40577

要回复问题请先登录注册