How to deal with Chinese when ArcGIS uses Python scripts for field calculations

Source: Internet
Author: User
Tags python script

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.