Problem:  Expression File (.exp) returns encoded string when read in Python

相关信息
Article ID: 40344
Bug Id: N/A
Software:
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

问题描述
When using the Select By Attributes dialog in ArcMap, expressions can be saved into an expression file (.exp). These files can be loaded into the Select By Attributes dialog for later use.

Although an .exp file can be opened and viewed with a text editor to see the stored expression, the same file returns a Unicode string if read into a variable in Python.

For example, an .exp file can be opened in Notepad to see that the expression is "OBJECTID" = 1, but if the same .exp file is read into Python it returns "\000O\000B\000J\000E\000C\000T\000I\000D\000"\000 \000=\000 \0001\000 for the expression.

原因
The .exp files are encoded.
已邀请:

易智瑞技术支持

赞同来自:

解决方案
To see the expression stored within the expression file, use Python's decode method with the utf-16 encoding scheme.
# Variable: Path to .exp file
text = r"C:\VM_SHARE\Expression.exp"

# Process: Open .exp file for reading
file = open(text, 'r')

# Process: Read first line of .exp file
line = file.readline()

# Process: Decode line
.exp = line.decode('utf-16', 'ignore')

# Message: Return message to display
print exp

# Process: Close file object
file.close()


原文链接
http://support.esri.com/en/kno ... 40344

要回复问题请先登录注册