Learn Python bytes every day

Source: Internet
Author: User

Learn Python bytes every day
Learn Python bytes every day

Use bytecode in Pythonb'xxx'. X can be represented by characters or ASCII code.\xnnIndicates that the number of nn characters ranges from 00-ff (hexadecimal) to 256.

Basic operations

The following describes the basic operations of a byte. It can be seen that it is very similar to a string:

In [40]: B = B "abcd \ x64" In [41]: bOut [41]: B 'abcd' In [42]: type (B) out [42]: bytesIn [43]: len (B) Out [43]: 5In [44]: B [4] Out [44]: 100 #100 in hexadecimal notation \ x64

If you want to modify a byte in a byte string, you must convert it to bytearray before modifying it:

In[46]: barr = bytearray(b)In[47]: type(barr)Out[47]: bytearrayIn[48]: barr[0] = 110In[49]: barrOut[49]: bytearray(b'nbcdd')
Relationship between byte and character

As mentioned above, bytes are very similar to characters. In fact, they can be converted to each other. Bytes can be converted to corresponding characters in some encoding form. Bytes can be converted to characters by using the encode () method, while characters can be converted to bytes by using the decode () method:

In [50]: s = "My life is short, I use Python" In [51]: B = s. encode ('utf-8') In [52]: bOut [52]: B '\ xe4 \ xba \ xe7 \ x94 \ x9f \ xe8 \ x8b \ xa6 \ xe7 \ x9f \ xad \ xef \ xbc \ x8c \ xe6 \ x88 \ x91 \ xe7 \ x94 \ xa8Python 'In [53]: c = s. encode ('gb18030') In [54]: cOut [54]: B '\ xc8 \ xcb \ xc9 \ xfa \ xbf \ xe0 \ xb6 \ xcc \ xa3 \ xac \ xce \ xd2 \ xd3 \ xc3python' In [55]: B. decode ('utf-8') Out [55]: 'Life is short, I use python' In [56]: c. decode ('gb18030') Out [56]: 'Life is short. I use Python 'In [57]: c. decode ('utf-8') Traceback (most recent call last): exec (code_obj, self. user_global_ns, self. user_ns) File"
  
   
", Line 1, in
   
    
C. decode ('utf-8') UnicodeDecodeError: 'utf-8' codec can't decode byte 0xc8 in position 0: invalid continuation byteIn [58]: B. decode ('gb18030') Out [58'
   
  

We can see that the characters and bytes parsed by different encoding methods are completely different. If different encoding methods are used for encoding and decoding, garbled characters may occur, or even conversion fails. Because each encoding method contains different types of bytes\xc8The maximum UTF-8 character is exceeded.

Application

For the simplest example, I want to crawl the content of a webpage. Now I want to crawl the page returned by Baidu when searching for Python. From the url, we can see that the encoding method is UTF-8, if the returned result is not decoded, It is a super long byte string. After correct decoding, a normal html page is displayed.

import urllib.requesturl = "http://www.baidu.com/s?ie=utf-8&wd=python"page = urllib.request.urlopen(url)mybytes = page.read()encoding = "utf-8"print(mybytes.decode(encoding))page.close()

Related Article

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.