DAY07 Python2 and Python3 coding

Source: Internet
Author: User

More articles: http://www.cnblogs.com/yuanchenqi/articles/5956943. Htmlhttp://www.diveintopython3.net/ strings.html need to know:1. In Python2 the default encoding is ASCII, python3 default is Unicode2.unicode divided into utf-32 (4 bytes), utf-16 (two bytes) , Utf-8 (1-4 bytes), so utf-16 is now the most commonly used
Unicode version, but in the file is still utf-8, because UTF8 save space 3. In Py3 encode, the transcoding will also change the string to bytes type, Decode will also turn bytes back to string while decoding.

For Python2 only

Python2:
#-*-coding:utf-8-*-__author__='Alex Li'ImportSYSPrint(sys.getdefaultencoding ()) MSG="I love Beijing"msg_gb2312= Msg.decode ("Utf-8"). Encode ("gb2312") GB2312_TO_GBK= Msg_gb2312.decode ("GBK"). Encode ("GBK")Print(msg)Print(msg_gb2312)Print(GB2312_TO_GBK)
Python 3:
#-*-coding:gb2312-*-#这个也可以去掉__author__='Alex Li'ImportSYSPrint(sys.getdefaultencoding ()) MSG="I love Beijing"#msg_gb2312 = Msg.decode ("Utf-8"). Encode ("gb2312")msg_gb2312 = Msg.encode ("gb2312")#The default is Unicode, no more decode, hi Big BenGb2312_to_unicode = Msg_gb2312.decode ("gb2312") Gb2312_to_utf8= Msg_gb2312.decode ("gb2312"). Encode ("Utf-8")Print(msg)Print(msg_gb2312)Print(Gb2312_to_unicode)Print(Gb2312_to_utf8)
4. Character encoding first say python2py2 the default encoding is the ASCII file at the beginning of the encoding declaration is to tell the program explaining the code in what encoding format to read this code into memory, because in memory, this code is actually
Bytes binary format, but even 2 binary streams can be converted to 2 streams in different encoding formats, you know? If the file header is declared in the#_*_coding:utf-8*_, can write Chinese, do not declare, Python in the processing of this code, according to ASCII, obviously error,
After adding this statement, the code inside is all Utf-8 formatted.In a#_*_coding:utf-8*_ case, you are declaring a variable if written name=u "Big Health", that character is Unicode format, without this u,
So the string you're declaring is the utf-8 format.Utf-8to GBK How to turn, UTF8 first decode into Unicode, then encode into GBK python3py3 in the default file encoding is UTF-8, so you can write the Chinese directly, do not need the file header declaration code, dry Beautiful The variable you declare is Unicode encoding by default, not UTF-8, because the default is Unicode (unlike in py2, you want to directly declare Unicode
Variable before adding a U), at this time you want to turn into GBK words, direct your_str.encode ("GBK") that can but py3, you are in Your_str.encode ("GBK"), the feeling seems to add an action, that is, encode data into the bytes, I rub,
This is a situation, because in Py3, str andBytes made a clear distinction, you can understand that bytes is a 2-in-one flow, you would say, I see not
010101 such a 2, that's because Python allows you to manipulate the data in the memory level and help you to do a layer of encapsulation, or you can directly see a
Heap 2 binary, can you see which character corresponds to which paragraph 2 binary? What the?  Your own conversion, come on, you even more than 2 digits of the number plus and minus operations are laborious, or save worry bar. Then you say, there seems to be a bytes in the Py2, yes, but py2 bytes only to str made an individual name (Python2 str is bytes, py3
STR is Unicode), there is no more than the same as py3 to show you a layer of packaging, but in fact, it is encapsulated inside. So let's say, whether it's 2 or three,
From hard disk to memory, the data format is 101,012 binary to-->b'\XE4\XBD\XA0\XE5\XA5\XBD'Bytes Type--turn to you with the specified code
Can understand the text encoding application more than the scene should be a reptile, many web sites on the Internet with a very miscellaneous encoding format, although the overall trend has become UTF-8, but it's still very miscellaneous,
So you need to do all kinds of transcoding when crawling Web pages.

DAY07 Python2 and Python3 coding

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.