First, Introduction
When you open a layer's property sheet in ArcGIS, you can calculate a field in the property sheet, but in general it is usually done by using the interface of the field calculator provided by ArcGIS for a simple, simplified assignment, without using the script to logically manipulate the field values. Since I've been learning a Python script recently, and just met a good friend who needs my assists (using ArcGIS mapping), that's it. This thought can be easy to deal with, did not expect to move a stone hit the foot, the following is how I was smashed foot it.
Two
Problem Description:Change the value of "Hunan" in the Test field to "Hunan province".
This logic is quite simple, using Python to write a corresponding method for:
def cal (value): if (value==' hunan '): return ' Province ' Else : return value
Run Python code in ArcGIS such as:
Note : The reference attribute field is worth the way! test!
Click OK, the results pop up an error, no hint specifically what is wrong, and finally found a bit of information, the original is Python when using Chinese must be transcoded.
Third, the solution
So the above code in the Chinese appear in the place to transcode it just fine. The changed code is as follows:
def cal (value): if (value==' hunan 'decode ('utf-8')): return ' Province '. Decode ('utf-8') Else: return value
There is no error, and what needs to be changed is to encode it by adding the decode (' Utf-8 ') method after the string "Hunan" and "province".
This solves the problem of handling Chinese in Python scripts in ArcGIS.
Iv. Summary
Using Python scripts in ArcGIS, you will need to decode (' Utf-8 ') to transcode them whenever you encounter Chinese.
In addition, the Python Script Editor provided in ArcGIS is super hard to use, so you can start with good python editing, write your logic code well, then copy it in, then run it quickly and easily.
How to deal with Chinese when ArcGIS uses Python scripts for field calculations