<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>

                ThinkChat2.0新版上線,更智能更精彩,支持會話、畫圖、視頻、閱讀、搜索等,送10W Token,即刻開啟你的AI之旅 廣告
                ## 問題 你想讀寫二進制文件,比如圖片,聲音文件等等。 ## 解決方案 使用模式為 `rb` 或 `wb` 的 `open()` 函數來讀取或寫入二進制數據。比如: # Read the entire file as a single byte string with open('somefile.bin', 'rb') as f: data = f.read() # Write binary data to a file with open('somefile.bin', 'wb') as f: f.write(b'Hello World') 在讀取二進制數據時,需要指明的是所有返回的數據都是字節字符串格式的,而不是文本字符串。類似的,在寫入的時候,必須保證參數是以字節形式對外暴露數據的對象(比如字節字符串,字節數組對象等)。 ## 討論 在讀取二進制數據的時候,字節字符串和文本字符串的語義差異可能會導致一個潛在的陷阱。特別需要注意的是,索引和迭代動作返回的是字節的值而不是字節字符串。比如: >>> # Text string >>> t = 'Hello World' >>> t[0] 'H' >>> for c in t: ... print(c) ... H e l l o ... >>> # Byte string >>> b = b'Hello World' >>> b[0] 72 >>> for c in b: ... print(c) ... 72 101 108 108 111 ... >>> 如果你想從二進制模式的文件中讀取或寫入文本數據,必須確保要進行解碼和編碼操作。比如: with open('somefile.bin', 'rb') as f: data = f.read(16) text = data.decode('utf-8') with open('somefile.bin', 'wb') as f: text = 'Hello World' f.write(text.encode('utf-8')) 二進制I/O還有一個鮮為人知的特性就是數組和C結構體類型能直接被寫入,而不需要中間轉換為自己對象。比如: import array nums = array.array('i', [1, 2, 3, 4]) with open('data.bin','wb') as f: f.write(nums) 這個適用于任何實現了被稱之為”緩沖接口”的對象,這種對象會直接暴露其底層的內存緩沖區給能處理它的操作。二進制數據的寫入就是這類操作之一。 很多對象還允許通過使用文件對象的 `readinto()` 方法直接讀取二進制數據到其底層的內存中去。比如: >>> import array >>> a = array.array('i', [0, 0, 0, 0, 0, 0, 0, 0]) >>> with open('data.bin', 'rb') as f: ... f.readinto(a) ... 16 >>> a array('i', [1, 2, 3, 4, 0, 0, 0, 0]) >>> 但是使用這種技術的時候需要格外小心,因為它通常具有平臺相關性,并且可能會依賴字長和字節順序(高位優先和低位優先)。可以查看5.9小節中另外一個讀取二進制數據到可修改緩沖區的例子。
                  <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>

                              哎呀哎呀视频在线观看