English

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

Description

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.

Cause

The .exp files are encoded.

Solution or Workaround

To see the expression stored within the expression file, use Python's decode method with the utf-16 encoding scheme.

Code:
# 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()