Python Chinese string interception problem

Source: Internet
Author: User

Web applications will inevitably intercept the need for strings, Python is easy to intercept English, but the interception of Utf-8 Chinese opportunity to intercept half caused some not garbled garbled. In fact, UTF8 intercept is very simple, here to share to everyone

Let's take a look at an example:

?
1 2 3 4 #-*-Coding:utf8-*-s = U ' Chinese intercept ' s.decode (' UTF8 ') [0:3].encode (' UTF8 ') # results U ' Chinese interception

Extended reading:

How many bytes does the Chinese character in UTF-8 occupy?

Up to 2 bytes: 0

3 bytes: Basically equivalent to GBK, including more than 21,000 Chinese characters

4 bytes: China, Japan and Korea super large character set inside the Chinese characters, there are more than 50,000

A UTF-8 number for 1 bytes

A UTF-8 English letter for 1 bytes

Find UTF-8 encoded data found that many of the posts said UTF-8 code, a Chinese character occupies 3 bytes, and some also made a proof, probably so, create a UTF-8 encoded text file without a BOM, which saved a few Chinese characters, and then view the size of the file. I think this kind of proof is not a bit convincing, because the UTF-8 is longer, 1-6 bytes, a small number of Chinese character detection is not that all Chinese characters are true.

Later I looked at the character Map-Chinese, found the correct answer, a few are Chinese characters each occupy 3 bytes, most occupy 4 bytes.

The above mentioned is the entire content of this article, I hope you can enjoy.

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.