Preface
The most important new feature of Python 3 is probably a clearer distinction between text and binary data. Text is always Unicode, represented by the STR type, and binary data is represented by the bytes type. Python 3 does not mix str and bytes in any implicit way, which makes the distinction between them particularly clear. You cannot stitch strings and byte packets, search for strings in a byte packet (or vice versa), or pass a string into a function with a byte packet (or vice versa).
How to create bytes type data in python3.0
Bytes ([1,2,3,4,5,6,7,8,9]) bytes ("Python", ' ASCII ') # string, encoded
First, set up an original string,
Python 3.2.3 (default, APR, 07:15:24) [MSC v.1500 + bit (Intel)] on Win32type "help", "copyright", "credits" or " License "For more information.>>> website = ' http://www.www.php.cn/' >>> type (website) <class ' str ' >>>> website ' http://www.www.php.cn/' >>>
Encode by utf-8, turn into bytes
>>> Website_bytes_utf8 = Website.encode (encoding= "utf-8") >>> type (Website_bytes_utf8) <class ' Bytes ' >>>> website_bytes_utf8b ' http://www.www.php.cn/' >>>
Encode by gb2312, turn into bytes
>>> website_bytes_gb2312 = Website.encode (encoding= "gb2312") >>> type (website_bytes_gb2312) < Class ' bytes ' >>>> website_bytes_gb2312b ' http://www.php.cn/' >>>
Decoded to string, default not filled
>>> website_string = Website_bytes_utf8.decode () >>> type (website_string) <class ' str ' >> >> website_string ' http://www.php.cn/' >>>>>>
Decoded to a string, using the gb2312 method
>>> website_string_gb2312 = Website_bytes_gb2312.decode ("gb2312") >>> type (website_string_gb2312) <class ' str ' >>>> website_string_gb2312 ' http://www.php.cn/' >>>