character encoding conversion

introduction:

Since the original computer was invented by the United States, the first set of computer character encoding is ascii, which contains a total of 255 characters (English characters and special characters)

With the development of science and technology, countries gradually have their own codes such as gbk in China, Shift-JIS in Japan, etc.

However, because each country has its own code, it is impossible to realize the perfect operation of the software in country a on the computer in country b.

There are two solutions: 1. Each computer in country b installs the character encoding table and interpreter corresponding to country a

        2. The codes of country a are all changed to the codes of country b

Therefore, unicode is born from the need, that is, the universal code

However, each character of unicode is two bytes, that is, if a piece of code is all in English, it will cause a huge waste of space, and it will also cause a waste of network transmission speed.

## One English is in ascii, occupying one byte, one Chinese is in unicode, occupying two bytes

       In unicode, two bytes In utf-8, three bytes

       In gbk, one byte

       In utf-8, one byte

Character encoding conversion:

      In python3, you only need to declare what encoding to write at the top, because python3 will automatically map the code you write to Unicode in memory according to the writing rules, and the Unicode mapping function automatically converts the written code to the current System default character writing rules

      But in python2, because of what kind of declaration code you write in, python2 is generated in what kind of code in memory, so automatic conversion cannot be achieved.

      If you need to convert, follow the rules

          #encoding:utf-8

          s = 'My God'

          s.decode('utf-8') ##Decode to unicode according to the said code rules

          s.encode('gbk') ##Encode to the desired form

          

      

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325903872&siteId=291194637