A detailed explanation of Chinese Python Problems

Source: Internet
Author: User

Before explaining the Python Chinese issue, let's first talk about What Python is, and there was a strong interest in Python in the past. Who knows this old friend once again has an unexpected problem with Chinese? In the code, the problem of Chinese Python is always bothering us ..

It is no wonder that we are not the Chinese who invented computers. Otherwise, computers all over the world now support and must support GBK. I am not the one who writes this article, but a kingfa programmer on the other side of the ocean, and the title is changed to studying the english problem in 'python '".

Let's face the real problems. Compared with java, the performance of Chinese problems in Python is more intense. "Fierce" means not to say it is more serious or difficult to solve. Only Python uses strict by default for decode and encode errors, that is, an error is reported directly, while java uses replace to handle them, therefore, a lot "?? ".

In addition, Python's default encoding is ASCII, while java's default encoding is consistent with the operating system's encoding. At this point, I think java is more reasonable. This is more friendly to programmers and reduces the frustration of newbies at the beginning, which is conducive to language promotion.

However, Python also has its own principle. After all, ASCII is the only character set supported by all platforms in the world, and the problem always occurs, it is better to face it earlier than to escape it. Okay. Now, let's talk about the symptoms of Chinese problems in Python. Before that, we should first understand that Python has two types of strings, each of which is a general string and each character is represented by 8 bits) and the Unicode string is represented by one or more bytes ).

They can be converted to each other and have a more comprehensive description. I will not talk about them here. Let's look at the following code:

 
 
  1. #-*-Coding: gb2312-*-# It must be in the first or second line.
  2. Print "------------- code 1 ----------------"
  3. A="A. I love you" 
  4. Print
  5. Print a. find ("I ")
  6. B=A. Replace ("love", "like ")
  7. Print B
  8. Print "-------------- code 2 ----------------"
  9. X="A. I love you" 
  10. Y=Unicode(X, "gb2312 ")
  11. Print y. encode ("gb2312 ")
  12. Print y. find (u "I ")
  13. Z=Y. Replace (u "", u "")
  14. Print z. encode ("gb2312 ")
  15. Print "--------------- code 3 ----------------"
  16. Print y

It is a non-ASCII character, and let's refer to pep-0263. PEP-0263Python Enhancement Proposal) The above is very clear, Python is aware of the international problem, and proposed a solution. According to the requirements above, we have the following code:

 
 
  1. ------------- Code 1 ----------------
  2. A. I love you
  3. 5
  4. A. I like you
  5. -------------- Code 2 ----------------
  6. A. I love you
  7. 3
  8. A. I like you
  9. --------------- Code 3 ----------------
  10. Traceback (most recent call last ):
  11. File "G: \ Downloads \ eclipse \ workspace \ p \ src \ hello. py", line 16, in<Module> 
  12. Print y
  13. UnicodeEncodeError: 'ascii 'codec can't encode characters in position 0-1: ordinal not in range (128)

We can see that by introducing the Python Chinese statement, we can normally use Chinese, and in code 1 and 2, the console can correctly print Chinese. However, it is obvious that the above Code also reflects many problems:
1. code 1 and 2 use different print methods. 1 is direct print, and 2 is encoded Before print.
2. In code 1 and 2, find the same character "I" in the same string and the results are different: 5 and 3)
3. An error occurs when unicode string y is directly printed in code 3. This is why code 2 must be encoded first)

  1. Introduction to Python system files
  2. How to correctly use Python Functions
  3. Detailed introduction and analysis of Python build tools
  4. Advantages of Python in PythonAndroid
  5. How to Use the Python module to parse the configuration file?

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.