The concept of coding
Encoding is the conversion of information from one format to another format. In other words, converting what we know into a binary format known to a computer is a coded behavior, and the computer decodes the contents of the binary format into what we know.
The first time the computer used encoding rules is ASCII code, ASCII code was first used by Americans. ASCII code uses a byte binary group to represent a character (since they only use 26 citation letters and some symbols, the original ASCII code even uses only 7 bit bits).
With the increasing popularity of computers, ASCII code is difficult to meet people around the world's use, in China there has been GB2312 and GBK encoding, using a two-byte binary group to represent a character (or even tough to occupy Latin America and other countries of the highest bit). Because of this, all over the world using their own coding, the respective software is not compatible, so the advent of the universal code.
The Universal Code (UNICODE) covers all the text in the world, which is too powerful, so is it convenient for us to use the universal code? But for Americans, they only have to use one byte to represent all the characters, and now it's a bit more than a byte, which makes the memory and hard disk wasted space, so the Unicode is optimized into the current utf-8 format. Utf-8 is a variable-length encoding, so now development tends to use this coding method. But there are still a lot of places that use GBK,ASCII encoding, so we need to know more about coding.
Code for Python2
The default encoding method in Python2 is ASCII code.
' Jeff ' # str type bytesname2 = u' Jeff ' # Change the string type to Unicode Print repr (name)print repr (name2) Run result '\xbd\ Xdc\xb7\xf2'u'\u6770\u592b'
In Python2, the STR string type is stored in memory as a bytes type, and the Unicode type string stores Unicode data.
Name ='Jeff' #name is a byte data typename2 = u'Jeff' #name is a Unicode data typeName3 = Name.decode ('UTF8') Name4= Name2.encode ('UTF8')Printtype (NAME3)Printtype (name4)Printrepr (name)Printrepr (name2)Printrepr (Name3)Printrepr (NAME4) Run results<type'Unicode'><type'Str'>'\xe6\x9d\xb0\xe5\xa4\xab' #str字符串类型的bytes数据u'\u6770\u592b' #Unicode字符串类型的Unicode数据u'\u6770\u592b' #str字符串类型解码成Unicode数据'\xe6\x9d\xb0\xe5\xa4\xab' #Unicode字符串类型编码成bytes数据Code for Python3
The default encoding method in Python3 is Utf-8.
Name = b'Jeff'name2='Jeff'Print(Type (name))Print(repr (name))Print(Type (name2))Print(repr (name2)) Run results<class 'bytes'>b'Jeff'<class 'Str'>'Jeff'
Python3 the STR string type in memory is Unicode data, and the bytes type string stores bytes data.
Name = b'Jeff'name2='Jeff'Name3=name.decode ('UTF8') Name4=name2.encode ('UTF8')Print(Type (name))Print(Type (name2))Print(Type (name3))Print(Type (name4))Print(repr (name))Print(repr (name2))Print(repr (name3))Print(repr (NAME4)) Run results<class 'bytes'><class 'Str'><class 'Str'><class 'bytes'>b'Jeff' #bytes类型字符串存储的bytes数据'Jeff' #str类型字符串存储的Unicode数据
'Jeff' #bytes类型字符串解码成Unicode数据b'\xe6\x9d\xb0\xe5\xa4\xab' #str类型字符串编码城bytes数据
A simple summary of the encoding bytes data is to facilitate the transmission and storage, and Unicode data convenient display, Python3 than python2 more clear the bounds of the byte and character, Python3 canceled the python2 of different types of string concatenation.
This is often the case with different coding methods.
Write a small program in Python,
#coding =utf8print (' Jeff ')
Open this file in Windows terminal.
This shows a bunch of garbled characters, which is why?
Because my Python3 default encoding method is Utf-8, and my Windows terminal default decoding method is GBK, lianxi1.py This file content in memory to utf-8 encoding way to write to the hard disk, in cmd execution, the CMD software default decoding method is GBK, with GBK way to decode the Utf-8 binary data, decoding out is a bunch of garbled, so the solution is either let cmd use utf-8 way to solve Code, or you can only let the Python interpreter use the GBK method to encode the file content into the hard disk.
# CODING=GBK Print (' Jeff ')
Python and encoding