What is python encoding and decoding?
Today, I would like to share with you the relevant knowledge of what python encoding and decoding is. The content is detailed and the logic is clear. I believe most people still know too much about this knowledge, so share this article for your reference. I hope you can get something after reading this article. Let's take a look at it.
Encoding and decoding
Encoding and decoding
Encode (Encoding)-get the byte corresponding to the plaintext encoding (binary)
Decode (Decoding)-decodes the encoded byte (binary) into the corresponding plaintext
#-*-encoding:utf-8-*-indicates that utf-8 coding is used when Chinese is available.
ASCII code does not support Chinese
Support for English, numbers and symbols
8 bits and 1 byte
Gbk International supports Chinese, English, numbers and symbols, and must be compatible with ASCII codes.
English 16 bits and 2 bytes
16 bits and 2 bytes in Chinese
Unicode universal code must be compatible with ASCII code
Support for Chinese and English, numbers and symbols
32 bits and 4 bytes in English
32 bits and 4 bytes in Chinese
The universal code with variable utf-8 length uses at least 8 bits.
English 8 bits 1 byte
European 16 bits 2 bytes
24 bits and 3 bytes in Chinese
ASCII code for python2 version
When the python3 program runs, it uses unicode to display everything.
Bytes type: bytes type is used for both transport and storage
Utf-8 encoding is used by default when pycharm storage
S = 'are you hungry'
S1 = s.encode ('gbk') # code, Gbk code, one Chinese character 2 two bytes
Print (S1) # b'\ xb6\ xf6\ xc1\ xcb\ xc2\ xf0',\ xb6 counts as one byte
S2 = s1.decode ('gbk') # Decoding
Print (S2)
Print (s.encode ('utf-8'))
A = 'boss'
Print (a.encode ('utf-8')) # b'\ xe5\ xa4\ xa7\ xe4\ xbd\ xac', two Chinese characters, six bytes, gbk decoding, two bytes one Chinese character, then gbk decoding should be three Chinese characters
Print (a.encode ('utf-8'). Decode (' gbk')) # Pulp, utf-8 coding, gbk decoding
# be careful to decode whatever code is used
B = 'China'
Print (b.encode ('gbk')) # Encoding-b'\ xd6\ xd0\ xb9\ xfa'
Print (b.encode ('gbk'). Decode (' gbk')) # encode and then decode-China
A = 'modesty'
B = 'modesty'
Print (id (a), id (b))
Ls = [1, 3, 4, 4, ""]
Ls2 = [1, 3, 4, 4, ""]
Print (id (ls), id (ls2)) # list memory address is different
Print (not ls is ls2) # True
Print (id (ls [- 1]), id (ls2 [- 1]) # extract elements, but the memory address of the same element is the same 47059648 47059648
Ls = [1, 3, 4, 4, ""]
Ls2 = [1, 3, 4, 4, 'Hapi']
Print (id (ls [- 1]), id (ls2 [- 1])) # 47059648 47061664
# the string above occupies a certain position in memory. For the second time, you only need to call the string under the memory address and no longer create it. Provincial memory
# when a string contains special characters, the memory address is different
A = [1pm 2pm 3]
B = a
C = b
Print (a = = c) # compare values, because the list is created only once, remember the question: has a new cache been created, the answer is no
Print (an is c) # compares the memory address. All three variables point to a list, so the memory address is the same.
# when comparing memory addresses, you need to see how many lists have been created. In this way, create a list once, with the same memory address, and create multiple lists with different addresses, but the string is not applicable.
A = [1, 3, 4]
B = [1pr 3pr 4]
C = b
Print (a = = c) # True
Print (an is b) # fasle
Print (an is c) # false
S = 'Hello'
Print (s.encode ('gbk')) # b'\ xc4\ xe3\ xba\ xc3\ xc2\ xf0'
C = b'\ xc4\ xe3\ xba\ xc3\ xc2\ xf0'
Print (c.decode ('gbk')) # Decoding
Print (s.encode ('utf-8')) # b'\ xe4\ xbd\ xa0\ xe5\ xa5\ xbd\ xe5\ x90\ x97'
Print (s.encode ('utf-8'). Decode (' gbk')) # raccoon decoder, decode whatever you use, this decoding is meaningless, that is all the content of this article "what is python encoding and decoding?" Thank you for reading! I believe you will gain a lot after reading this article. The editor will update different knowledge for you every day. If you want to learn more knowledge, please pay attention to the industry information channel.