python coding - decoding

 

In py3 only two types of data: str bytes

str:   deposit unicode ( Unicode ) coding - universal

bytes: deposit is 16 hexadecimal

1.str

= S 'ehllo Qing Li'   --- "which are stored in memory in coding unicode

2.bytes (010101010 computer knowledge)

There is a disk, network transmission, etc. are bytes type --- because bytes type from the bottom closer --- the computer can recognize it.

3. bytes transferred by the str ---- >> called coding type

How turn?

-------- bytes () method

= S 'ehllo Qing Li'

b1 = bytes (s, 'utf8') ----- to s bytes in turn into type utf8

## Each country has its own kind bytes type, but is recognized worldwide as easy to use utf8

bytes (s, 'gbk') ----- s to turn into bytes of type gbk

## gbk is Zan Chinese characters bytes type

------ encode () built-in method

b2 = s.encode ( 'utf8') ----- to s bytes in turn into type utf8

b2 = s.encode ( 'gbk') ----- to s bytes in turn into type gbk

 

4. --- "to str called by the bytes decoded

How turn?

----- str () built-in method

s = str (b2, 'gbk') --- b2 into the bytes of type gbk

# What bytes bytes type of coding on what type of decoding

---- decode () method

s2=b2.decode(‘gbk’)

 

Note: the str turn into bytes type have to have a regular ( UTF / GBK / ..... )

Guess you like

Origin www.cnblogs.com/dbslinux/p/11240490.html