Encoding and decoding in python3 (excellent understanding) and python3 Encoding

Source: Internet
Author: User

Encoding and decoding in python3 (excellent understanding) and python3 Encoding

Encoding and decoding are for data. What can we do with data? It is used for display, storage, and transmission;

To store and transmit data, we certainly hope that the smaller the data, the better, so we invented the UTF-8 data encoding display. It intelligently represents English in one byte, and European characters in two bytes, chinese characters are represented in three bytes.

If the data is displayed, you do not need to consider the data size! Therefore, the unicode standard is used for display. Each character occupies two bytes. Whether it is Chinese or English or in other countries.

 

If you want to display the data, the size does not matter, so you should use international standards, that is, unicode. So when you are typing, all the display on the screen is decoded with unicode, however, many of you in China use gbk Decoding for display, such as cmd in Windows .. Well, now you write a good word. You want to save it. Of course, the smaller the data, the better. So you need to encode it. You can use the encode method to encode it, if you encode your words into UTF-8 encoding format, the stored data will become smaller!

How to encode it into UTF-8?

Format: object. encode ('utf8 ')

Encode the object into a UTF-8 data type for storage.

Of course, you can also encode it into a domestic gbk encoding format. By the way, the Chinese version of ^_^ is supported.

Object. encode ('gbk ')

This object is encoded as a domestic gbk code to save the data.

**************************************** **************************************** ****************************

In case you want to see the words you wrote. That is, display data on the screen.

If you used Global UTF-8 encoding today, You need to decode and decode it,

Format: object. decode ('utf8 ')

 

If you used gbk encoding in China today, decode it.

Format: object. decode ('gbk ')

Congratulations, you can watch the words you wrote on the screen.

-------------------------------------------------------------------------------

Cmd garbled question?

For example, if you write a string in python3, the interpreter of 2: python3 automatically encodes the string into a unicode data type, unicode data can be decoded by any shell and can be decoded by gbk of cmd. But what if you are in python2? Why?

# Coding: utf8s = 'xiaoming'

Input this code in python2 and put it in the cmd running result to display garbled characters. Why?

Because when you write this string, the interpreter in python2 will encode it into word throttling according to the declared utf8, and this file is also saved in utf8 format, the key is that when the byte is transmitted to cmd, cmd is decoded in gbk format by default, and the byte stream is saved in utf8 format. The two formats are different, so garbled characters may occur.

How can this problem be solved?

The first method is to encode the interpreter in python2 into a word throttling according to the declared gbk format?

#__ Author _ = 'admin' # coding: gbks = 'xiaoming 'print (s)

The result is a row!

 

Try the second method and start with the code?

Can I encode this string into gbk format? Can I not perform cmd decoding?

Step 1: because James has been encoded as UTF-8 byte stream data by the python2 interpreter, we first decode s. decode ('utf') to unicode data.

Step 2: encode s_unicode.encode ('gbk') for Xiaoming of this unicode data in gbk format ')

The third step is printable:

Code:

# Coding: utf8s = 'xiaoming's _ unicode = s. decode ('utf8') s_gbk = s_unicode.encode ('gbk') print (s_gbk)

In fact, we can also optimize this code!

Since unicode data can be decoded in any format. There is no need to encode it in gbk format;

Therefore, we only need to perform the first step, and there is no need to perform the second step.

Both methods are successful!

**************************************** **************************************** **************************************** **************************************** **********************************

In python3, there are two data types: str and bytes. (Str is an encoded string, unicode is used for all nations, and gbk is used for China. Maybe jbk ^_^ is used in Japan .. Bytes is a byte and can only contain 0-characters in the ascll code)

In python3, it is specified that the str string displayed on the screen is represented by unicode data. The bytes data used to store and transmit data is better understood by the bytes data computer. But it is hard to understand;

In python3, data is converted to the byte type while encoding; byte type is byte, which can only be printed out of the ascll code;

Decoding converts the byte type to a string;

 

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.