<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                合規國際互聯網加速 OSASE為企業客戶提供高速穩定SD-WAN國際加速解決方案。 廣告
                # 每天學點Python之bytes Python中的字節碼用`b'xxx'`的形式表示。x可以用字符表示,也可以用ASCII編碼形式`\xnn`表示,nn從00-ff(十六進制)共256種字符。 ### 基本操作 下面列舉一下字節的基本操作,可以看出來它和字符串還是非常相近的: ~~~ In[40]: b = b"abcd\x64" In[41]: b Out[41]: b'abcdd' In[42]: type(b) Out[42]: bytes In[43]: len(b) Out[43]: 5 In[44]: b[4] Out[44]: 100 # 100用十六進制表示就是\x64 ~~~ 如果想要修改一個字節串中的某個字節,不能夠直接修改,需要將其轉化為bytearray后再進行修改: ~~~ In[46]: barr = bytearray(b) In[47]: type(barr) Out[47]: bytearray In[48]: barr[0] = 110 In[49]: barr Out[49]: bytearray(b'nbcdd') ~~~ ### 字節與字符的關系 上面也提到字節跟字符很相近,其實它們是可以相互轉化的。字節通過某種編碼形式就可以轉化為相應的字符。字節通過encode()方法傳入編碼方式就可以轉化為字符,而字符通過decode()方法就可以轉化為字節: ~~~ In[50]: s = "人生苦短,我用Python" In[51]: b = s.encode('utf-8') In[52]: b Out[52]: b'\xe4\xba\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]: c Out[54]: b'\xc8\xcb\xc9\xfa\xbf\xe0\xb6\xcc\xa3\xac\xce\xd2\xd3\xc3Python' In[55]: b.decode('utf-8') Out[55]: '人生苦短,我用Python' In[56]: c.decode('gb18030') Out[56]: '人生苦短,我用Python' In[57]: c.decode('utf-8') Traceback (most recent call last): exec(code_obj, self.user_global_ns, self.user_ns) File "<ipython-input-57-8b50aa70bce9>", line 1, in <module> c.decode('utf-8') UnicodeDecodeError: 'utf-8' codec can't decode byte 0xc8 in position 0: invalid continuation byte In[58]: b.decode('gb18030') Out[58]: '浜虹敓鑻︾煭錛屾垜鐢≒ython' ~~~ 我們可以看到用不同的編碼方式解析出來的字符和字節的方式是完全不同,如果編碼和解碼用了不同的編碼方式,就會產生亂碼,甚至轉換失敗。因為每種編碼方式包含的字節種類數目不同,如上例中的`\xc8`就超出了utf-8的最大字符。 ### 應用 舉個最簡單的例子,我要爬取一個網頁的內容,現在來爬取用百度搜索Python時返回的頁面,百度用的是utf-8編碼格式,如果不對返回結果解碼,那它就是一個超級長的字節串。而進行正確解碼后就可以顯示一個正常的html頁面。 ~~~ import urllib.request url = "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() ~~~
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看